We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Associated model's attributes converted to string

Hi all, I have several models, with associations, defined as following:

<?php
class Page extends \Phalcon\Mvc\Model {
public function initialize()
    {
        $this->belongsTo('voting_id', 'Voting', 'id', array('alias' => 'Voting', 'foreignKey' => true));
        $this->hasMany('id', 'PageParameter',     'page_id',   array('alias' => 'PageParameters', 'foreignKey' => true));
        $this->hasMany('id', 'AnswerParameter',   'page_id', array('alias' => 'AnswerParameters', 'foreignKey' => true));
        $this->hasMany('id', 'QuestionParameter', 'page_id', array('alias' => 'QuestionParameters', 'foreignKey' => true));
    }
}
<?php 
class PageParameter extends \Phalcon\Mvc\Model {
    public function initialize()
    {
        $this->belongsTo('page_id',      'Page',      'id', array('alias' => 'Page', 'foreignKey' => true));
        $this->belongsTo('parameter_id', 'Parameter', 'id', array('alias' => 'Parameter', 'foreignKey' => true));
    }
}
<?php
class Parameter extends \Phalcon\Mvc\Model implements \Serializable
{
    /**
     * Parameter type
     *
     * @var integer
     */
    protected $type;

    public function initialize()
    {
        $this->hasMany('id', 'AnswerParameter',   'parameter_id', array('alias' => 'AnswerParameters', 'foreignKey' => true));
        $this->hasMany('id', 'PageParameter',     'parameter_id', array('alias' => 'PageParameters', 'foreignKey' => true));
        $this->hasMany('id', 'QuestionParameter', 'parameter_id', array('alias' => 'QuestionParameters', 'foreignKey' => true));
        $this->hasMany('id', 'VotingParameter',   'parameter_id', array('alias' => 'VotingParameters', 'foreignKey' => true));
    }
}

Then, while doing this cycle,

foreach ($this->page->pageParameters as $pageParameter) {
            if ($pageParameter->getType() == self::PAGE_PARAMETER_TYPE
                && isset($this->parameters[$pageParameter->getIdentifier()])
                && $pageParameter->parameter->getType() == $this->parameters[$pageParameter->getIdentifier()]->getType()
            ) {
                $dbParameter = $pageParameter->parameter;
                $dbParameterValue = $dbParameter->getValue();
                $dbParameter->setDefault($this->parameters[$pageParameter->getIdentifier()]->getDefault());
                $dbParameter->setValue($dbParameterValue);
                $this->parameters[$pageParameter->getIdentifier()] = $dbParameter;
            }
        }

the $dbParameter is loaded from database, but $dbParameter->getType() is returning string, even though in the database the field is integer, and if the Parameter is instantiated, the value is set to integer constant.

Is there something to be set somewhere that I'm missing, or is it a bug?

Thanks for your help with this.



43.9k

Hi,

In Parameter Model, you're using getter and setter, so maybe, to force integer value for $type, maybe you can try this:

  public function getType()
    {
        return (int)$this->type;
    }


794

Hi, thanks for the answer.

I'm doing this, to ensure the value will be int, but this is not the solution to the original problem I guess.

The main problem remains, is this the correct behavior, or is it a bug.