Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
DB as_object()
  • I have some code which returns a query as_object() This works, but I want to be able to get the _has_many() children from the returned object. Is this possible? Below is my code: $query = DB::select_array(array('*'))
    ->from('job')
    ->as_object()
    ->where(blah = ?, value); $results = $query->execute();
    foreach ($results as $record) {
    var_dump($record->mymodel);
    } Where mymodel : <?php
    class Model_Job extends Orm\Model
    {
    protected static $_table_name = 'job';
    protected static $_primary_key = array('jobID'); protected static $_has_many = array('mymodel' => array(
    'model_to' => 'Model_dbtable2',
    'key_from' => 'jobID',
    'key_to' => 'jobID'
    ));
    }
    It just returns null, no errors though. Thanks in advance, Chris
  • I don't understand what you want. You're using DB calls, which have nothing to do whatsoever with ORM or ORM models...
  • So the object returned isn't one of my ORM classes? Its just a standard class. Okay, I thought it might be able to communicate and return my ORM class as the result. Are they completely separate modules? Thanks, Chris
  • The DBAL implemented by the DB classes is just a query builder. It runs queries and returns results, either as arrays or as standard objects. You can use it to populate ORM model objects though, see http://docs.fuelphp.com/classes/database/usage.html#/results
  • Yeah I thought that's what I was doing, but I missed the object name from the as_object() call. Never mind, I switched the code to use the ORM instead of the DB classes. I have managed to get the relationships working quite nicely now. Thanks for your help.
  • $result = DB::select()->from('users')->as_object('Model_Users')->execute();


    How do i access Model_Users object?

    all these return error.
    $result['Model_Users'];
    $result->Model_Users;
    $result::$Model_Users;
    $Model_Users;

    i think FuelPHP document should have more and clear example, it is hard to understand.
  • $result will be an array of Model_Users objects. So the same result as Model_Users::find('all') (with this query).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion