Models
<?php
namespace Zzz\Models;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
use Phalcon\Mvc\Model\Validator\Uniqueness;
class Rules extends Model
{
/**
*
* @var integer
*/
public $id;
/**
*
* @var integer
*/
public $time;
public function initialize()
{
$this->belongsTo('time', 'Zzz\Models\AliasesTime', 'id', array(
'alias' => 'aliasestime'
));
}
}
namespace Zzz\Models;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
use Phalcon\Mvc\Model\Validator\Uniqueness;
class AliasesTime extends Model
{
/**
*
* @var integer
*/
public $id;
/**
*
* @var string
*/
public $name;
public function initialize()
{
$this->hasMany("id", "Zzz\Models\AliasesTimeContent", "aliastimeId", array(
"alias" => "aliasestimecontent",
"foreignKey" => array(
"action" => Relation::ACTION_CASCADE
)
));
$this->hasMany("id", "Zzz\Models\Rules", "ruletime", array(
"alias" => "rules",
"foreignKey" => array(
"message" => "The alias cannot be deleted because other internet policy rule are using it"
)
));
}
}
namespace Zzz\Models;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Relation;
class AliasesTimeContent extends Model
{
/**
*
* @var integer
*/
public $id;
/**
*
* @var string
*/
public $starttime;
/**
*
* @var string
*/
public $endtime;
/**
*
* @var integer
*/
public $aliastimeId;
public function initialize()
{
$this->belongsTo("aliastimeId", "Zzz\Models\AliasesTime", "id", array(
"alias" => "aliasestime",
"foreignKey" => true
));
}
}
Code
....
$rules = Rules::find();
if ($rules) {
$timeAlias = array();
foreach ($rules as $policy) {
# Here I am required to conduct a new forearch
# For I can not access the table values AliasesTimeContent directly by model
# When the find returns multiple results
# $policy->aliasestime->aliasestimecontent->starttime
foreach ($policy->aliasestime->aliasestimecontent as $content) {
echo $content->id;
echo $content->starttime;
echo $content->endtime;
echo $content->aliastimeId;
}
}
}
...
The code is works, but is logging warnings in the Apache error log to " foreach ($policy->aliasestime->aliasestimecontent as $content) {"
[Fri Jan 23 16: 16: 54.857380 2015] [: error] [pid 27759] [client 192.168.122.1:41581] PHP Warning: Invalid argument supplied for foreach ()
Perhaps there is a proper way to get these values using the model or some method of the framework, but I could not find in the documentation. Has anyone had to do that? Any tips?