[Solar-talk] Sql_Model insert not working!

Rodrigo Moraes rodrigo.moraes at gmail.com
Mon Apr 23 02:56:18 PDT 2007


On 4/22/07, Jeff Surgeson wrote:
> Did not work for me, still cannot insert records, as far as I see
> $item = $this->_news->fetchNew()->toArray();
> wont work as $item needs to be an object not an array?
>
> How did you go about getting a record insert to work?

The toArray() method just make an array version of the data (which I
needed for caching, since PDO instances can't be serialized and thus
cached), but didn't solved the focus problem to insert new records.

Since it is not possible to execute fetchNew() on a record, you need a
new instance of the model class to merge default values with the new
record values. So, on _insert(), the previously commented code could
be:

    // keep a copy of the data for manipulation (filters, etc)
    $this->_data = array_merge(
        Solar::factory(get_class($this))->fetchNew()->toArray(),
        $this->_data
    );

Then you will have a record with default values and the new values,
and insertion will work. I think it is simple as that: add toArray()
and use a new instance ('master' focus) instead of $this->fetchNew()
('record' focus). It works nicely, then.

Let me know if you have problems, because this is solution I'm using
now and I'm afraid that I'm missing something.

rodrigo


More information about the solar-talk mailing list