Page 1 sur 1

Acceder à un sous ensemble d'une classe

Posté : 07 juin 2010, 19:13
par avalokitech
Bonjour, je suis presque débutant.
Voici ci-dessus le résumé de ma classe. J'ai compris que pour accéder à id je devais coder $this->_data->id.
Mon problème est d'accéder à l'objet dificultes pour la mettre dans un tableau. La boucle foreach ne fonctionne pas
Merci de votre aide.
Cordialement.
Avalokitech.

class RecettessModelRecettes extends JModel
{

/**
* Constructor that retrieves the ID from the request
*
* @access public
* @return void
*/
function __construct()
{
parent::__construct();

$array = JRequest::getVar('cid', 0, '', 'array');
$this->setId((int)$array[0]);
}//function

/**
* Method to set the Recettes identifier
*
* @access public
* @param int Recettes identifier
* @return void
*/
function setId($id)
{
// Set id and wipe data
$this->_id = $id;
$this->_data = null;
}//function


/**
* Method to get a record
* @return object with data
*/
function &getData()
{
// Load the data
if (empty( $this->_data ))
{
$query = ' SELECT * FROM #__recettes '.
' WHERE id = '.$this->_id;
$this->_db->setQuery( $query );
$this->_data = $this->_db->loadObject();

if($this->_data) //If there is a result, convert some data
{
$this->setDifficultes();
}

}
if (!$this->_data) {
$this->_data = new stdClass();
$this->_data->id = 0;
$this->_data->greeting = NULL;
$this->_data->recette_nom = NULL;
$this->_data->categorie = NULL;
$this->_data->titre = NULL;
$this->_data->image = NULL;
$this->_data->ingredients = NULL;
$this->_data->preparation = NULL;
$this->_data->personne = NULL;
$this->_data->temps_prepa = NULL;
$this->_data->temps_cuisson = NULL;
$this->_data->temps_attente = NULL;
$this->_data->difficulte_id = NULL;
$this->_data->cout = NULL;
$this->_data->datcre = NULL;
$this->_data->Datmaj = NULL ;
$this->_data->username = NULL;
$this->_data->published = NULL;
$this->setDifficultes();
}
// print_r($this->_data);
dump( $this->_data, 'recettes' );

foreach ($this->recettes->difficultes as $difficulte)
{
echo $difficulte->id;
if($this->recettes->difficulte_id==$difficulte->id)
echo $difficulte->type;

}

return $this->_data;
}//function

function setDifficultes()
{
//Prend la liste des difficulte
$query = ' SELECT * '.
' FROM #__difficultes';
$this->_db->setQuery( $query );
$this->_data->difficulte = $this->_db->loadObjectList();
}



}// class