Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

binding form select with doctrine object

I have a problem binding forms with relational objects.

I work with Doctrine and Phalcon2

In the entity I have a getter that returns and object and not and id or string.

     /**
     * @return Laboratorio
     */
    public function getLaboratorioSangrePositivoTKI()
    {
        return $this->laboratorioSangrePositivoTKI;
    }

And when phalcon try to bind the entity to the form it returns this error:

Catchable fatal error: Object of class DoctrineProxies__CG__\Identify\Entities\Laboratorio could not be converted to string in /var/www/clients/client1/web1/web/app/common/cache/volt/app/backend/views/hospital/formHospitalInterno.phtml on line 64

I know that problem is that phalcon try to set a default value with the object, but it doesn't work because I need to get the id.

I try to solve adding using to select attribute, but it doesn't work:

$this->add(new Select('laboratorioTejido', $laboratoriosTejido, [
            'class' => 'form-control filter',
            'using' => ['id','nombre'],
            'useEmpty' => true,
            'emptyText' => 'Selecciona ...',
        ]));

1. Can I change the binding for one element?

2. If no, also i'm thinking if can I to setup a blacklist to the binding?

3. What's the best way to solve this issue?

Thanks a lot! Fidel



77.7k

i think phalcon components ( including Forms ) are made to work with phalcon orm ( makes sence ? )

so i would guess that the best way is to extend the Select class and overwrite the functions that are using phalcon orm.

edited Jan '18

I prefer ignore the binding for this element, the project is in production and your proposal is too hard. Thanks

i think phalcon components ( including Forms ) are made to work with phalcon orm ( makes sence ? )

so i would guess that the best way is to extend the Select class and overwrite the functions that are using phalcon orm.

Hi @itemvirtual why you don't pass an array?

$form->add(
    new Select(
        'telephoneType',
        [
            'H' => 'Hogar',
            'C' => 'Movil',
        ]
    )
);

// in your case
$form->add(
    new Select(
        'telephoneType',
        array_column($query->getQuery()->getArrayResult(), 'nombre', 'id')
    )
);

What do you think? Good luck

Hi! I pass an array. But my problem isn't with the creation, is with the binding, because Phalcon tries to bind and object to the Select and it needs an integer to set the Default Value.

Thanks Fidel

Hi @itemvirtual why you don't pass an array?

$form->add(
   new Select(
       'telephoneType',
       [
           'H' => 'Hogar',
           'C' => 'Movil',
       ]
   )
);

// in your case
$form->add(
   new Select(
       'telephoneType',
       array_column($query->getQuery()->getArrayResult(), 'nombre', 'id')
   )
);

What do you think? Good luck

What the need to use doctrine when the framework had a native orm which no need any include and clearly better than doctrine in any point ...

Hi, this is not my question.

What the need to use doctrine when the framework had a native orm which no need any include and clearly better than doctrine in any point ...



21.6k
Accepted
answer

well you must use the undocumented function getCustomValue($elemName, $entity, $data)

Here you will do something like this

class YourForm extends Phalcon\Forms\Form {

    // all your code here

    public function getCustomValue($name, $entity, $data) {
        if ($name === 'yourSelectName') {
            return 'what you want for select here';
        }

        if ($entity) {
            return $entity->toArray()[$name];
        }

        return $data[$name] ?? null;
    }
}

Other way you can pass an object as entity but itsn't an Model. Form docs quote: "An entity such as a model/collection/plain instance or just a plain PHP class can be linked to the form in order to set default values in the form's elements or assign the values from the form to the entity easily" more info forms doc

Good luck

Thanks!!!! It works perfectly! Can I send you a beer? :D

well you must use the undocumented function getCustomValue($elemName, $entity, $data)

Here you will do something like this

class YourForm extends Phalcon\Forms\Form {

  // all your code here

  public function getCustomValue($name, $entity, $data) {
      if ($name === 'yourSelectName') {
          return 'what you want for select here';
      }

      if ($entity) {
          return $entity->toArray()[$name];
      }

      return $data[$name] ?? null;
  }
}

Other way you can pass an object as entity but itsn't an Model. Form docs quote: "An entity such as a model/collection/plain instance or just a plain PHP class can be linked to the form in order to set default values in the form's elements or assign the values from the form to the entity easily" more info forms doc

Good luck