We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Skipping relations table and getting records directly.

Hello!

Getting along with my project here. I have a question though for which I have failed to find instructions.

I have a table of "group"(s), and a table of "item"(s). These are synced through a relational table (groups_items), since groups can contain an arbitrary number of items, and items can belong to an arbitrary number of groups. Now, what I want to do is this:

$group->getItems(array("id > ?0", array(0 => 10)));

I can't do this though, since there's no direct relation between groups and items. I can do:

$group->getGroupsItems();

And then try to filter my results from the relations, but I'd like to do this directly in the ORM. The main problem here is the arguments I guess, even though I can't do 'getItem' and return all related records from the ResultsetSimple either. Can someone please hint me in the right direction?

Thanks!

Best regards, dimhoLt



7.0k

Hi, I am not sure I understand the Problem correctly.

I think you should write your own getGroupsItems() method.

doing something like this:


class Group extend Phalcon\Mvc\Model
{
    public fucntion initialize()
    {
         $this->hasMany("id", "GroupsItems", "group_id", array("alias" => "GroupsItemsRelations")));
    }

    public function getGroupsItems()
    { 
         $relations = $this->groupsItemsRelations();

         $items = array();

         foreach($relations as $rela) {
               $items[] = $rela->getItem();
         }
         return $items;
    }
}


22.6k

Hello Songtao, and thanks for your response.

Yeah, that's almost exactly what I'm doing right now, but if I want to filter the items, I'd have to do something like this:

// For the example, let's assume $arguments looks like this:
// array(
//    "field" => "id",
//     "operator" => ">",
//     "comparisonValue" => 10
// );
// Although I'd like to do SQL / PHQL selections like when using ::find()
public function getItems($arguments) {
    $relations = $this->getGroupsItems();

   $ret = array();
   foreach($relations as $relation) {
        $tmpItem = $relation->getItem();

        switch($arguments["operator"]) {
            case ">":
                if($tmpItem->{$arguments["field"]} > $arguments["comparisonValue"]) {
                    $ret[] = $tmpItem;
                }
                break;

            case "<":
                // Etc...
                break;
        }
   }

   return $ret;
}

As you see, it quickly becomes tedious and bloated... I'd like to do these things directly in the ORM.

If I was unclear, I'll try to improve my description of the problem; I'd like to do exactly what your example does, but I'd like to get the related records without having to loop through them in PHP after having fetched them from the DB:

// Assume $arguments = array(
//   "conditions" => "id > ?0",
//   "bind" => array(0 => "10")
// );
public function getItems($arguments) {
    $relations = $this->getGroupsItems();
    $items = $relations->getItem($arguments); // Returns an array of Item-objects.
    return $items;
}

Do you understand the issue? I have another case where I built a completely custom query to get the fields I want with proper joins etc, which is fast and works well, but I feel there should be functionality for this, since Phalcon has such an impressive interface, and this should be a common problem.

Best regards, dimhoLt



22.6k

Seems very much like what I was looking for! Gonna try this out =) Thanks!