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

Multiple relations and left joind models

Hi all, I have model

class Event
    extends Model
{
    public $id;
    public $created_at;
    public $user_id;
    public $type_id;
    public $product_id;

    public function initialize()
    {
        $this->hasOne('product_id', 'Product\\Model\Product', 'id', array(
             'foreignKey' => true,
             'alias' => 'Product'
        ));
        $this->hasOne('user_id', 'User\\Model\User', 'id', array(
            'foreignKey' => true,
            'alias' => 'User'
        ));
        $this->hasOne('type_id', 'User\\Model\Events\Type', 'id', array(
            'foreignKey' => true,
            'alias' => 'Type'
        ));
        $this->hasMany('user_id', 'User\\Model\Follow\User', 'author_user_id', array(
            'foreignKey' => true,
            'alias' => 'Followers'
        ));
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap() {
        return array(
            'id' => 'id',
            'created_at' => 'created_at',
            'user_id' => 'user_id',
            'type_id' => 'type_id',
            'product_id' => 'product_id'
        );
    }
} 

How can i left join on alias when use method >leftJoin or what i should use? Thanks for your help!



11.8k
edited Oct '14

try

$phql = "SELECT Event.*, Followers.* FROM Event LEFT JOIN Followers";
$result = $manager->executeQuery($phql);

P.S. https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/phql.html?highlight=left%20join#creating-queries-using-the-query-builder