Problem Relations On Seach()


(Lucasdeabreu) #1

Let’s start with my tables:




create table IF NOT EXISTS module (

    id              int         not null primary key auto_increment,

    name         varchar(50) not null

);

create table IF NOT EXISTS status (

    id              int         not null primary key auto_increment,

    name         varchar(50) not null

);

create table IF NOT EXISTS module_status (

    id              int         not null primary key auto_increment,

    module_id       int         not null,

    status_id       int         not null,

    foreign key (module_id) references module (id),

    foreign key (status_id) references status (id)

);

create table IF NOT EXISTS company (

    id              int         not null primary key auto_increment,

    name            varchar(100) not null,

    cnpj            varchar(14) not null unique,

    phone           varchar(14),

    email           varchar(100),

    module_status_id int not null,

    foreign key (module_status_id) references module_status (id)

);



For each module have many status and one status can be on many company

What I want is, on company/admin.php make possible to see the status.name and filter them.

Soo what i tried to do:




public function search()

{

        $criteria=new CDbCriteria;

                

        $criteria->compare('t.id',$this->id);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('cnpj',$this->cnpj,true);

        $criteria->compare('phone',$this->phone,true);

        $criteria->compare('email',$this->email,true);

        

        //$criteria->compare('module_status_id',$this->module_status_id);

                

        $criteria->compare('moduleStatus.status.name', $this->moduleStatus->status->id, true);


        $criteria->with=array('moduleStatus');

                

        return new CActiveDataProvider($this, array(

                'criteria'=>$criteria,

        ));

}



The error came on this line:


        $criteria->compare('moduleStatus.status.name', $this->moduleStatus->status->id, true);

The error:


  Trying to get property of non-object

Anyone could help me?


(Developer) #2

I would do it like this, far more efficient:

http://www.mattiressler.com/customising-cgridview-using-a-function-to-display-related-data/

Just substitute what I have used as "organisations" with your "status" table data.

Here is another way:

http://www.mattiressler.com/using-class-properties-to-minimise-database-queries-and-code-execution/


(Lucasdeabreu) #3

Thx for the reply, I tried to do as you on your blog but something went wrong.

I’d like to know better why you did like that, and there is a way to do like I was trying?


(Developer) #4

It depends on the data, but the essential idea is to execute a single query, rather than a query for every single grid row being returned.