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

How to set the alias data while using '' using '' attribute in Phalcon\Forms\Element\Select

I trying to show wallet names (alias data) in the select box.....In the "using" attribute i want set the wallettype_id with wallettype_name which is defined in other model...How to do this??


$wallet = new Select('wallet[]', CorporateWalletRelation::find(array("([corporate_id] =:corporate_id:)                                                                                AND ([is_active] = :is_active:)", 'bind' => array('corporate_id' => $corporate_id, 'is_active' => 1))),
array(
 'using' => array('wallettype_id', 'wallettypes->wallettype->name'), 
 'multiple' => 'multiple',
));
$wallet->setAttribute('id', 'wallettypes');
$wallet->setLabel('Wallet Type');
$this->add($wallet);


43.9k

Hi,

let us see your CorporateWalletRelation and Wallet models

edited Dec '16
use Phalcon\Mvc\Model;

class CorporateWalletRelation extends Model {
    public $id;
    public $wallettype_id;
    public $corporate_id;
    public $is_active

    public function initialize() {
        $this->setSource('employee_corporate_relation');
        $this->belongsTo('wallet_id', 'Wallet', 'id', array('alias' => 'walletdata'));
    }
}

class  Wallet extends Model {
    public $id;
    public $walletname;
    public $amount;
    public $status;

    public function intialize(){
        $this->setSource('wallet');
    }
}


43.9k

you can't get related models data using the basic ORM tool. Use phql or query builder to get them in a sigle query (using join) or if you still want to use ORM, first get your corprateWalletRelation data and then build the data array you want to pass to the select constructor:


// something like this

$relations = CorporateWalletRelation::find(array("([corporate_id] =:corporate_id:)  AND ([is_active] = :is_active:)", 'bind' => array('corporate_id' => $corporate_id, 'is_active' => 1)));
$datas = [];
foreach($relations as relation){
    $datas[] = ['wallettype_id' => $realtion->wallettype_id, 'name' => $relation->walletdata->walletname];
}

$wallet = new Select('wallet[]', $datas,
array(
 'using' => array('wallettype_id', 'name'), 
 'multiple' => 'multiple',
));