Skipping relations table and getting records directly.


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:


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?


Best regards, dimhoLt


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

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

doing something like this: ```php

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;




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;

            case "<":
                // Etc...

   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: ```php // 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


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