Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using ORM with a "friends" table
  • I have a "friendships" table with columns "user1" and "user2" which links two User models together using many_many. What I'm trying to do is use ORM to get all friends of a user - the problem is that the user could be user1 or user2, so I would have to use conditionals in the join or run two queries with a UNION.

    I got it working using \DB::query() with union, but then when I try to do as_object('\\Model\\User')), I get "Could not execute \Model\User::__construct()". So I figure maybe it will work in ORM but I don't know how to use ORM with a conditional join statement or union query. 

    Any ideas?

    Here's the query that I'm generating that is throwing the "could not execute" error:

    (SELECT `friend`.* FROM `friendships` INNER JOIN `users` `friend` ON `friendships`.`user1`=`friend`.`id` WHERE `friendships`.`user2`='4' ORDER BY `friend`.`username`) UNION (SELECT `friend`.* FROM `friendships` INNER JOIN `users` `friend` ON `friendships`.`user2`=`friend`.`id` WHERE `friendships`.`user1`='4' ORDER BY `friend`.`username`) LIMIT 0, 36

    I'm just running it like this:

    $results = $query->param('offset', $pagination->offset)->param('limit', $pagination->per_page)->as_object('\\Model\\User')->execute()->as_array();
  • What does that model's constructor look like?

    Because as_object() using an ORM model is not a problem, that should work fine.
  • I replaced \Model\User with \Orm\Model and got the same error. I know there's programming in \Orm\Model to account for hydration occurring before the constructor is called, but maybe something is missing?

    I narrowed it down to this:

    public function action_test() {
    $sql = "(SELECT `friend`.* FROM `friendships` INNER JOIN `users` `friend` ON `friendships`.`user1`=`friend`.`id` WHERE `friendships`.`user2`='4' ORDER BY `friend`.`username`) UNION (SELECT `friend`.* FROM `friendships` INNER JOIN `users` `friend` ON `friendships`.`user2`=`friend`.`id` WHERE `friendships`.`user1`='4' ORDER BY `friend`.`username`)";
    var_dump(\DB::query($sql, \DB::SELECT)->as_object('\\Orm\\Model')->execute()->as_array());
    exit();
    }

    When I run that query in the command line I get two rows. In PHP I get that "cannot execute __construct" error. Any ideas? In the meantime I'm going to play around in \Orm\Model and see if I can figure this out.

  • I just commented out the whole constructor in \Orm\Model and got an error, appropriately, that there's no table "models" in the database, when trying to run static::properties() in the set() method. I haven't been getting this error before, so this says to me that it's trying to run the constructor before doing the native hydration...does this make any sense?


  • UPDATE:

    There were two columns in the database that I did not have in the $_properties array - this was not an issue when using find() since it selected all the properties, instead of "*". When I used the asterisk, it would grab the fields that were not defined as properties, and throw an out of bounds error.

    What was confusing is that even though it was throwing an out of bounds exception, the exception displayed by FuelPHP was just the exception returned from mysql_fetch_object. This may be an internal thing and unfixable, but I bet other people could be pulling their hair out trying to figure out what's wrong with their constructor when the culprit is elsewhere.
  • HarroHarro
    Accepted Answer
    You can't use ORM\Model directly, it's a base class.

    As you observed, to_object() uses the selected drivers native command to create the objects, in this case "mysql_fetch_object". Looks like it can't deal with the exception being thrown, nothing much we can do about it.

Howdy, Stranger!

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

In this Discussion