[Solar-talk] trouble with $row->save()

Paul M Jones pmjones at solarphp.com
Sun Jul 23 13:58:02 PDT 2006


Hi Rodrigo,

> I've almost finished a "persistent auth" adapter. The only thing
> missing is to save the user row back with a new cookie token, but I'm
> in trouble using $row->save(), where row is a Sql_Row object returned
> from a Sql_Select::fetch('all') (like in Auth_Adapter_Sql::_verify()):
> ...
>         // get the results
>         $rows = $select->fetch('all');
>         if (count($rows) == 1) {
>             $row = $rows->current();
> ...
>         $row[$this->_config['token_col']] = $token;
>         //$row->setSave(???);
>         $row->save();
> ...
>
> Maybe I need to call setSave($obj) before save()? If yes, what exactly
> should be $obj? Or: what is the easiest way to get it done?

There's two ways to do this.

1.  To use an Solar_Sql_Row object to its fullest capacity, you need  
to call $row->setSave() first.  The object you pass into setSave()  
needs to have a save() method to handle inserts and updates to the  
proper table(s).  Take a look at lines 420+ in Solar/Content/ 
Abstract.php ...

         // get the row
         $row = $select->fetch('row');
         $row->setSave($this);
         return $row;

... and examine the save() method starting around line 530.  Although  
we used a Solar_Sql_Select tool to get back a row in fetchWhere(),  
there's no domain logic attached to the row, it's just data.  We  
attach $this as the source for domain logic (i.e., a save() method)  
using setSave($this).  Does that make sense?

2.  As an alternative, you can leave the row object "dumb" and issue  
commands directly to the database with the row data.  E.g., you can  
build an insert or update query manually and bind the row object.  E.g.:

         // $sql is a Solar_Sql object.
         // $table is the table name.
         // $row is the row data.
         //
         // just need a where clause to say which row.
         // be sure to quote it securely.
         $where = $sql->quoteInto('id = ?', $row->id);
         $sql->update($table, $row, $where);

Does this help at all?


--

Paul M. Jones  <http://paul-m-jones.com>

Solar: Simple Object Library and Application Repository
for PHP5.   <http://solarphp.com>

Savant: The simple, elegant, and powerful solution for
templates in PHP.   <http://phpsavant.com>




More information about the solar-talk mailing list