Search in relationship model

What are the possible parameters for the model belongsTo? I need to make a filter for the related table column and is not working.

    $this->belongsTo(
        'id',
        'Model',
        'id',
        [
            'alias' => 'newObjectName',
            'conditions' => 'sName= "teste"',
            'order'      => 'sName DESC',
        ]
    );
edited May '15

I don't think this is the correct use of relationship model.

  • Parameter 1 : supposed to be the column of your current model
  • Parameter 2 : supposed to be the full name of your model with its namespace (only if you defined a namespace)
  • Parameter 3 : supposed to be the column of the other model for the MySQL JOIN
  • Parameter 4 : supposed to be an array of parameters like the alias, the foreignKey (an array containing an 'action' key and a 'message' key)

E.g :

$this->belongsTo('bid', 'Fymh\Api\Models\Bands', 'id', [ 'alias' => 'Band', 'foreignKey' => [ 'message' => 'The band id does not exists on the Bands model' ] ]);

To retrieve data from the database with my defined model, I have to do :

Fymh\Api\Models\Bands::find();

Now you want to pass parameters like a condition to your MySQL query so here we go :

Fymh\Api\Models\Bands::find(array( 'conditions' => 'sName= "teste"', 'order' => 'sName DESC' ));

Hi samijnih,

What I need is something similar to last post of user "phalcon" this following link:

https://github.com/phalcon/cphalcon/issues/926

But besides the "order" parameter, accurate "conditions", is possible?

Hi samijnih,

What I need is something similar to last post of user "phalcon" this following link:

https://github.com/phalcon/cphalcon/issues/926

But besides the "order" parameter, accurate "conditions", is possible?


I don't think this is the correct use of relationship model.

  • Parameter 1 : supposed to be the column of your current model
  • Parameter 2 : supposed to be the full name of your model with its namespace (only if you defined a namespace)
  • Parameter 3 : supposed to be the column of the other model for the MySQL JOIN
  • Parameter 4 : supposed to be an array of parameters like the alias, the foreignKey (an array containing an 'action' key and a 'message' key)

E.g :

$this->belongsTo('bid', 'Fymh\Api\Models\Bands', 'id', [ 'alias' => 'Band', 'foreignKey' => [ 'message' => 'The band id does not exists on the Bands model' ] ]);

To retrieve data from the database with my defined model, I have to do :

Fymh\Api\Models\Bands::find();

Now you want to pass parameters like a condition to your MySQL query so here we go :

Fymh\Api\Models\Bands::find(array( 'conditions' => 'sName= "teste"', 'order' => 'sName DESC' ));

edited May '15

Hello,

Sorry for the late anwser..

Two things : 1. I saw your link 1. I did some tests

First test :

    // one band has many info (due to the language)
   $this->hasMany('id', 'Fymh\Api\Models\BandInfo', 'bid', [
       'alias'      => 'Info',
       'foreignKey' => [
           'action'  => Relation::ACTION_CASCADE,
           'message' => 'The info cannot be deleted'
       ],
       'order'      => 'language ASC'
   ]);

It do not works for me too

Second test (phalcon's way on Github)

   // one band has many info (due to the language)
  $this->hasMany('id', 'Fymh\Api\Models\BandInfo', 'bid', [
      'alias'      => 'Info',
      'foreignKey' => [
          'action'  => Relation::ACTION_CASCADE,
          'message' => 'The info cannot be deleted'
      ],
      'params' => [
          'orders' => 'language ASC'
      ]
  ]);

It works ..!

Final test

   // one band has many info (due to the language)
  $this->hasMany('id', 'Fymh\Api\Models\BandInfo', 'bid', [
      'alias'      => 'Info',
      'foreignKey' => [
          'action'  => Relation::ACTION_CASCADE,
          'message' => 'The info cannot be deleted'
      ],
      'params' => [
          'conditions' => 'language = "FR"'
      ]
  ]);

It do not works ..!

As you see, it only works with 'order' but not with 'conditions'.

I think we should submit an issue on the github because it's a pity :(

Hi samijnih,

What I need is something similar to last post of user "phalcon" this following link:

https://github.com/phalcon/cphalcon/issues/926

But besides the "order" parameter, accurate "conditions", is possible?


I don't think this is the correct use of relationship model.

  • Parameter 1 : supposed to be the column of your current model
  • Parameter 2 : supposed to be the full name of your model with its namespace (only if you defined a namespace)
  • Parameter 3 : supposed to be the column of the other model for the MySQL JOIN
  • Parameter 4 : supposed to be an array of parameters like the alias, the foreignKey (an array containing an 'action' key and a 'message' key)

E.g :

$this->belongsTo('bid', 'Fymh\Api\Models\Bands', 'id', [ 'alias' => 'Band', 'foreignKey' => [ 'message' => 'The band id does not exists on the Bands model' ] ]);

To retrieve data from the database with my defined model, I have to do :

Fymh\Api\Models\Bands::find();

Now you want to pass parameters like a condition to your MySQL query so here we go :

Fymh\Api\Models\Bands::find(array( 'conditions' => 'sName= "teste"', 'order' => 'sName DESC' ));