how to show the alias data for the variables in volt.

i working with model(shown below)......

use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Validation\Validator\PresenceOf as PresenceOfValidator;

class Employee extends Model
{

    /*
     *Id of the employee in db 
     *
    */

      public $id;

      /**
     * Name of corporate employee
     */
    public $name;
    /**
     * UserName of corporate employee
     */
    public $user_name;
    /**
     * Mobile Number of corporate employee
     */
    public $mdn;
    /*
     * delete
     */
    public $delete;




    public function initialize()
    {
        $this->setup(array(
        'notNullValidations'=>false,
        ));

        $this->setSource("employee");
        $this->hasManyToMany('id', 'employee', 'employee_id', 'type_id', 'Type', 'id', array(
                  'alias' => 'type'
                  ));
        $this->hasManyToMany('id', 'EmployeeCorporateRelation', 'employee_id', 'corporate_id', 'Corporate', 'id', array(
                  'alias' => 'corporate'
                  ));
    }

    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'name',
            new PresenceOfValidator([
            'message' => 'Name is required'
        ]));

        $validator->add(
            'user_name',
            new PresenceOfValidator([
            'message' => 'UserName is required'
        ]));

        $validator->add(
            'mdn',
            new PresenceOfValidator([
            'message' => 'Mobile number is required'
        ]));

        $validator->add(
            'user_name',
            new UniquenessValidator([
            'message' => 'Sorry, That username is already taken'
        ]));

        $validator->add(
            'mdn',
            new UniquenessValidator([
            'message' => 'Sorry, That mdn is already exists'
        ]));

        return $this->validate($validator);
    }
    }

now even i have a form for this.... while displaying the data (.volt file) i want to display the alias data for the variables for which alias has set

{% for element in form %}
    {% if is_a(element, 'Phalcon\Forms\Element\Hidden') %}
        {{ element }}
    {% else %}
        <div class="form-group">
            {{ element.label() }}
            {{ element.render(['class': 'form-control']) }}
        </div>
    {% endif %}
{% endfor %}

for variables like corporate id i want to the show it's name rather than its id. How can i do it. Hope you understand my problem!!! Please help!!



30.7k

Hi,

I usually use plurals in the alias when having HasMany or HasManyToMany relations:

$this->hasManyToMany('id', 'EmployeeCorporateRelation', 'employee_id', 'corporate_id', 'Corporate', 'id', array(
              'alias' => 'corporates'
              ));

if you've got an employe objet, you can access the related corporate objects (ManyToMany) like this:

{% for corporate in employee.corporates %}
    {{ corporate.name }}
{% endfor %}
edited Nov '16

but the problem is i am sending the value from the form....and i am rendering by running a for loop!! {% for element in form %} {% if is_a(element, 'Phalcon\Forms\Element\Hidden') %} {{ element }} {% else %} <div class="form-group"> {{ element.label() }} {{ element.render(['class': 'form-control']) }} </div> {% endif %} {% endfor %}

as i am running a for loop i don,t know for which variable alias has set.



30.7k

I 'do not understand the problem

Show us your Phalcon\Forms\Form From.

edited Nov '16

<?php

              use Phalcon\Forms\Form;
              use Phalcon\Forms\Element\Text;
              use Phalcon\Forms\Element\Select;
              use Phalcon\Forms\Element\Check;
              use Phalcon\Forms\Element\Hidden;
              use Phalcon\Forms\Element\Password;
             use Phalcon\Validation\Validator\PresenceOf;

               class EmployeeCorporateRelation extends Form {

public function initialize($entity = null, $options = array(), $employee_id = NULL, $user_group = NULL) {

    //id
    if (isset($options['edit'])) {

        $this->add(new Hidden("id"));
    }
    // Name
    if ($user_group != NULL) {
        $name = new Text('name', Employee::find("id='$employee_id'"), array(
            'using' => array('id', 'name'),));
        $name->setLabel('Employee Name');
        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'Name is required'
                    ))
        ));
        $this->add($name);

        // UserName
        $user_name = new Text('user_name', Employee::find("id='$employee_id'"), array(
            'using' => array('id', 'user_name'),));
        $user_name->setLabel('Username');
        $user_name->addValidators(array(
            new PresenceOf(array(
                'message' => 'UserName is required'
                    ))
        ));
        $this->add($user_name);

        // Mobile Number
        $mdn = new Text('mdn', Employee::find("id='$employee_id'"), array(
            'using' => array('id', 'mdn'),));
        $mdn->setLabel('Mobile Number');
        $mdn->addValidators(array(
            new PresenceOf(array(
                'message' => 'Please Enter Mobile Number'
                    ))
        ));
        $this->add($mdn);
    }
    // Corpoarte Id of Employee
    $empcorpid = new Text('empcorpid');
    $empcorpid->setLabel('Employee\'s Corporate ID');
    $empcorpid->addValidators(array(
        new PresenceOf(array(
            'message' => 'Employee\'s corporate id is required'
                ))
    ));
    $this->add($empcorpid);

    // Corpoarte Id of Employee
    $department = new Text('department');
    $department->setLabel('Employee\'s Corporate ID');
    $department->addValidators(array(
        new PresenceOf(array(
            'message' => 'Please enter department'
                ))
    ));
    $this->add($department);

    // Mobile
    $band = new Text('band');
    $band->setLabel('Band');
    $band->addValidators(array(
        new PresenceOf(array(
            'message' => 'Please enter department'
                ))
    ));
    $this->add($band);
    if ($user_group != NULL) {
        // Department
        $usergroup = new Text('user_group', UserGroup::find("id='$user_group'"), array(
            'using' => array('id', 'name'),));
        $usergroup->setLabel('User Group');
        $usergroup->addValidators(array(
            new PresenceOf(array(
                'message' => 'Please, mention the User Group of the employee'
                    ))
        ));
        $this->add($user_group);
    }
}

}

when i do this i am getting an error...saying Wrong number of parameters. Actually my problem is to show data of multiple models (not all the data) in a single view.



30.7k

hi

when i do this i am getting an error...saying Wrong number of parameters

do you mean when you instantiate the form : $form = new EmployeeCorporateRelation; ?

Actually my problem is to show data of multiple models (not all the data) in a single view

this can be done by preparing all your models objects in the controller action and then pass them to the view with $this->view->setVars()

so maybe with your controller action, it would be clearer ...