Doctrine: Define Model Values โ€‹โ€‹as Arrays

I have an array of values โ€‹โ€‹that I want to update using my model.

Doctrine_Access provides the setArray function, which is almost completely consistent with me, except that it takes care of values โ€‹โ€‹that have no fields in the model. I want them to be ignored.

A small example. Say we have a User table with a field username.

$user = new User();
$user->setArray(array('username'=>'xyz'))->save();

That would work!

$user = new User();
$user->setArray(array('username'=>'xyz','anotherKey'=>'anotherValue'))->save();

This is not true. I want the Doctrine to simply ignore anotherKey if there is no related area. It is assumed that I do not want to filter my arrays before updating my model.

What is the cleanest and easiest way to do this?

+3
source share
2 answers

Doctrine_Record:: fromArray() . , , ...

+1

:

class Address extends Doctrine_Record {

    public static function factory() {
        return new Address();
    }

public function findById($id) {
       $findObject = Doctrine::getTable('Address')->findOneByid($id);
       return $findObject;
     }
....

  $address = Address::factory()
        ->findById(13)->set('name', 'new data')->set('anotherfield','another data')->save();
-1

Source: https://habr.com/ru/post/1717856/


All Articles