Relazioni Tra Tabelle

Salve, ho un problema per quanto concerne la relazione tra tre tabelle. Ovvero

Tabella Listino

Id

Id_Servizio

Tabella Servizi

Id

Id_Settore

Tabella Settori

Id

Settore

All’interno del model Listino sono riuscito a relazionare la tabella Listino con quella dei Servizi, ma non riesco a relazionare la tabella Servizi con quella Settore. Vorrei fare in modo che il model Listino assuma la visualizzazione dei seguenti campi:

-Id

-Servizio

-Settore

Come posso fare. Grazie anticipatamente.

non capisco bene cosa vuoi fare, creare delle relazioni sui models?

tipo


	

public function relations()

	{

		return array(

....

			'translations' => array(self::HAS_MANY, 'Translation', 'post_id'),

...

		);

	}

?

Allora, ho creato il model Listino su cui ho realizzato la seguente relazione:


public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'servizi' => array(self::BELONGS_TO, 'Servizi', 'IdServizio'),

		);

	}



per visualizzare una vista che mi dia il nome completo del servizio e non l’Id. Inoltre ogni servizio contiene un riferimento alla tabella settore, per cui vorrei estrapolare anche il settore d’appartenenza.

La vista sarà composta:

Id (listino) | Servizio | Settore | Prezzo

Dal model Listino come faccio a creare la relazione tra Servizi e Settore??

Spero di essere stato più chiaro.

Nel modello listino che io sappia non puoi costruire relazioni tra servizio e settore, dovresti costruire tale relazione dovresti farlo sulle relazioni degli altri due modelli.

Poi utilizzi gli activerecord per ottenere i dati. Ma per una query simile utilizzerei direttamente sql. Se mi mandi in pm la struttura delle tabelle magari ti mando un paio di soluzioni. Ora sono col cell ma domani ti posso aiutare

Potresti mostrarci il codice non funzionante?




<?php


/**

 * This is the model class for table "listinoprezzi".

 *

 * The followings are the available columns in table 'listinoprezzi':

 * @property integer $Id

 * @property integer $UserId

 * @property integer $IdServizio

 * @property integer $IdListino

 * @property double $Prezzo

 *

 * The followings are the available model relations:

 * @property Users $user

 * @property Servizi $idServizio

 * @property Listino $idListino

 * @property Listino[] $listinoprezzis

 */

class ListinoPrezzi extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Listino the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'listinoprezzi';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('UserId, IdServizio, IdListino, Prezzo', 'required'),

			array('UserId, IdServizio, IdListino', 'numerical', 'integerOnly'=>true),

			array('Prezzo', 'numerical'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('Id, UserId, IdServizio, IdListino, Prezzo', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'user' => array(self::BELONGS_TO, 'Users', 'UserId'),

			'servizi' => array(self::BELONGS_TO, 'Servizi', 'IdServizio'),

			'listini' => array(self::BELONGS_TO, 'Listino', 'IdListino'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'Id' => 'Codice Listino',

			'UserId' => 'User',

			'IdServizio' => 'Id Servizio',

			'IdListino' => 'Id Listino',

			'Prezzo' => 'Prezzo',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

                

                $criteria->join = 'INNER JOIN Servizi ON Servizi.Id=t.Id INNER JOIN Settore ON Settore.Id = Servizi.IdSettore';

		$criteria->compare('Id',$this->Id);

		$criteria->compare('UserId',Yii::app()->user->id);

		$criteria->compare('IdServizio',$this->IdServizio);

                

		$criteria->compare('IdListino',$this->IdListino);

		$criteria->compare('Prezzo',$this->Prezzo);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

        

        public function getSettore()

        {

            return $this->servizi->settore->Settore;

        }

}



Ho provato anche questa soluzione ma restituisce il seguente errore:

La proprietà "Servizi"."settore" non è definita.

Non so più come procedere!

[PROBLEMA RISOLTO]

il metodo era:

public function getSettore()

    {


        return &#036;this-&gt;servizi-&gt;Settore-&gt;Settore;


    }

avevo scritto Settore con la s minuscola!!

Grazieeeeeeeeee sensorario!!!! :D

:slight_smile: hahaha bene allora