Quantcast

inserting with Zend_Db_Table/Row

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

inserting with Zend_Db_Table/Row

jimh
Using the latest trunk (895), I am able to insert using the following method

class DbUser extends Zend_Db_table{}

$users = new DbUser();
$data = array('fname'=>'test', 'lname'=>'test2');
$users->insert($data);

but the following does not work, based on example from http://www.alexatnet.com/Blog/Index/2006-06-22/programming-a-data-layer-classes

$users = new DbUser();
$user->fetchNew();
$user->fname = 'test';
$user->lname = 'test2';
$user->save();

I get no error, but nothing is inserted into the db and $user->save() returns 0.  Any tips?

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

Rodrigo Moraes
On 7/11/06, James Hartford wrote:
> Using the latest trunk (895), I am able to insert using the following method
>
> class DbUser extends Zend_Db_table{}

With this class name, your table name must be 'db_user'. If you want
to override this default, define a property $_name:

protected $_name = 'users';

Also if you have something different from 'id' for the primary key,
override the default:

protected $_primary = 'another_primary_key';

I hope this helps.
rodrigo moraes / brazil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

jimh
Actually I did do that, but I just didn't list the properties in the previous email

class DbUser extends Zend_Db_Table{
    protected $_name = 'users';
    protected $_primary = 'user_id';
}

But regardless of the table/primary key names, shouldn't the second approach work if the first approach worked?  Not sure if I am doing something wrong or if there is a bug with using the second approach.

thanks for the suggestion

On 7/11/06, Rodrigo Moraes <[hidden email]> wrote:
On 7/11/06, James Hartford wrote:
> Using the latest trunk (895), I am able to insert using the following method
>
> class DbUser extends Zend_Db_table{}

With this class name, your table name must be 'db_user'. If you want
to override this default, define a property $_name:

protected $_name = 'users';

Also if you have something different from 'id' for the primary key,
override the default:

protected $_primary = 'another_primary_key';

I hope this helps.
rodrigo moraes / brazil
 

Using the latest trunk (895), I am able to insert using the following method

class DbUser extends Zend_Db_table{}

$users = new DbUser();
$data = array('fname'=>'test', 'lname'=>'test2');
$users->insert($data);

but the following does not work, based on example from <a href="http://www.alexatnet.com/Blog/Index/2006-06-22/programming-a-data-layer-classes" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://www.alexatnet.com/Blog/Index/2006-06-22/programming-a-data-layer-classes

$users = new DbUser();
$user->fetchNew();
$user->fname = 'test';
$user->lname = 'test2';
$user->save();

I get no error, but nothing is inserted into the db and $user->save() returns 0.  Any tips?

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

Rodrigo Moraes
On 7/11/06, James Hartford wrote:
> Actually I did do that, but I just didn't list the properties in the
> previous email

Have you set a default adapter? Otherwise you have no connection.
Check section 6.4.2 here:
http://framework.zend.com/manual/en/zend.db.table.html

cheers,
rodrigo moraes / brazil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

jimh
Yeah, I set the default adapter, and everything seems to work fine except when trying to insert.  I can even use the same code with the find method to update an existing record.  The following works fine:

$users = new DbUser();
$user->find(1);
$user->fname = 'test';
$user->lname = 'test2';
$user->save();

but this does not:

$users = new DbUser();
$user->fetchNew();
$user->fname = 'test';
$user->lname = 'test2';
$user->save();


On 7/11/06, Rodrigo Moraes <[hidden email]> wrote:
On 7/11/06, James Hartford wrote:
> Actually I did do that, but I just didn't list the properties in the
> previous email

Have you set a default adapter? Otherwise you have no connection.
Check section 6.4.2 here:
http://framework.zend.com/manual/en/zend.db.table.html

cheers,
rodrigo moraes / brazil

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

Rodrigo Moraes
In reply to this post by jimh
On 7/11/06, James Hartford wrote:

> but the following does not work, based on example from
> http://www.alexatnet.com/Blog/Index/2006-06-22/programming-a-data-layer-classes
>
> $users = new DbUser();
> $user->fetchNew();
> $user->fname = 'test';
> $user->lname = 'test2';
> $user->save();
>
> I get no error, but nothing is inserted into the db and $user->save()
> returns 0.  Any tips?

Ah. I think the point is: there is no method save() in Zend_Db_Table.
There is insert() and update(), and perhaps you have created save() or
the guy from the tutorial have, but we know nothing about it. The
tutorial should be fixed to include the new method, which could be
something like:

    public function save(&$data)
    {
        if(!isset($data[$this->_primary])) {
            return $this->insert($data);
        } else {
            $where = $this->db->quoteInto($this->_primary . ' = ?',
$data[$this->_primary]);
            unset($data[$this->_primary]);
            return $this->update($data, $where);
        }
    }

cheers,
rodrigo moraes / brazil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: inserting with Zend_Db_Table/Row

Simon Mundy
There is a 'save()' method for a Db_Table_Row, however:-

$users = new DbUser();
$record = $user->fetchNew();
$record->fname = 'test';
$record->lname = 'test2';
$record->save();

That should work (fingers crossed!)

> On 7/11/06, James Hartford wrote:
>> but the following does not work, based on example from
>> http://www.alexatnet.com/Blog/Index/2006-06-22/programming-a-data- 
>> layer-classes
>>
>> $users = new DbUser();
>> $user->fetchNew();
>> $user->fname = 'test';
>> $user->lname = 'test2';
>> $user->save();
>>
>> I get no error, but nothing is inserted into the db and $user->save()
>> returns 0.  Any tips?
>
> Ah. I think the point is: there is no method save() in Zend_Db_Table.
> There is insert() and update(), and perhaps you have created save() or
> the guy from the tutorial have, but we know nothing about it. The
> tutorial should be fixed to include the new method, which could be
> something like:
>
>    public function save(&$data)
>    {
>        if(!isset($data[$this->_primary])) {
>            return $this->insert($data);
>        } else {
>            $where = $this->db->quoteInto($this->_primary . ' = ?',
> $data[$this->_primary]);
>            unset($data[$this->_primary]);
>            return $this->update($data, $where);
>        }
>    }
>
> cheers,
> rodrigo moraes / brazil

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Loading...