Search code examples
phppropel

Propel method update is undefined?


I'm trying to run the following code:

$post = PostQuery::create()
    ->findPk($id);

$post->update($params);

The method is taken from http://propelorm.org/documentation/03-basic-crud.html, but I receice an error:

Fatal error: Uncaught exception 'PropelException' with message 'Call to undefined method: update' in /usr/share/php/propel/om/BaseObject.php:426

What could I've done wrong in this case? I can delete successfully with the delete-method using in the same way:

$post = PostQuery::create()
        ->findPK($id);

$post->delete();

Update

I tried the mentioned solutions, the first one works well, but not the second one.

$post = PostQuery::create()
    ->filterById($id)
    ->update($params);

This now throws another error

Fatal error: Uncaught exception 'PropelException' with message 'Cannot fetch ColumnMap for undefined column phpName: id' in /usr/share/php/propel/map/TableMap.php:384

although the schema.xml looks correct to me:

 <table name="post" phpName="Post" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>

I don't know what could be the problem here as I am able to fetch the id by using the find()-method and

$post->getId();

while looping through.


Solution

  • You can update this way.

    In your case, you should do something like this (if $params is a key => value array with modified value):

    $post = PostQuery::create()
        ->findPk($id);
    
    $post->fromArray($params);
    $post->save();
    

    Or you can do it quickly:

    $post = PostQuery::create()
        ->filterById($id)
        ->update($params);
    

    Your solution doesn't work because you already retrieve the object using find(). If you want to update an object you already retrieved, you have to use save() method (and not update()). But if you don't rerieve the object, you can make an update in one query, using the update() method.