Re: DB_DataObject - Insert()
| From: | Alan Knowles | Date: | Fri, 23 May 2003 15:10:26 +0000 |
| Subject: | Re: DB_DataObject - Insert() | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-5574@lists.php.net to get a copy of this message | ||
Barry Steele wrote:
Hi, I've run into a confusing issue with DB_DataObject, and it requires a bit of explanation. I have number of tables (mysql) - Beaches -- [beach_key,,,,,{cols}] primary key = beach_key varchar(10)
car_parks [id,{cols - some are enum('y','n') ] primary key = id int and beach_car_park [beach_key, car_park_id] I need to be able to deal with the case where a user is looking at details about the beach (BTW - soon we'll have every beach in Oz on the net in major detail) We are in this case interested in the car_parks The form passes the following $_POST variables :- Output from print_r($_POST) : Array ([action] => insert [beach_key] => nsw209 [car_park_id] => [location] => blah blah [car_park_type] => sealed [sealed_parking_area] => N [disabled_parking] => Y [bus_parking] => N [car_park_size] => 11-50 [beach_visible] => Y [dist_to_beach] => 50 [Submit] => Submit )
In the PHP script I do :-
$carPark->setFrom($_POST);
$carPark->debugLevel(5);
then $id = $carPark->insert();
print_r( $carPark );
print("<br> <h2> Result from insert = $id </h2> <br>");
this will work on mysql if you have defined the primary key 'id' as autoincrement - other databases should be using sequences as the first defined keys (if they are integers) = there is another option ignore_sequence_keys = ALL|tablename,tablename ..
** yes this needs more documentation.. (have a look at the example.ini for details.)
The following result comes back Validation OK - Inserting new car park data sql QUERYINSERT INTO car_parks (location , car_park_type , disabled_parking , bus_parking , car_park_size , beach_visible , dist_to_beach )VALUES ('blah blah' , 'sealed' , 0 , 0 , '11-50' , 0 , 50 ) OK - you are sending 'Y/N' into DataObjects - it cant handle that unless you use v.0.19 with PHP4.3.2RC3/4 with the overload extension and define function setDisabled_parking($value) { $this->disabled_parking = ($value == 'Y'); return true; } - docs for this are at http://devel.akbkhome.com/peardoc2/package.database.db-dataobject.db-dataobject.settersgetters.html if you have 4.3.2RC3 or greater it is enabled by default, and setFrom relays into the set methods. otherwise you will have to override the setFrom method and manually manipulate any of the Y/N elements into integers..
query DONE QUERY dataobject_car_parks Object ( [_DB_DataObject_version] => 1.0 [__table] => car_parks [N] => 0 [_database_dsn] => [_database_dsn_md5] => [_database] => absamp_test [_condition] => [_group_by] => [_order_by] => [_limit] => [_data_select] => * [_DB_resultid] => [_link_loaded] => [_join] => [_lastError] => [id] => [location] => blah blah [car_park_type] => sealed [sealed_parking_area] => [disabled_parking] => Y [bus_parking] => N [car_park_size] => 11-50 [beach_visible] => Y [dist_to_beach] => 50 ) Result from insert = the nest step is supposed to be to insert the intersecting entity record but as you see the insert() doesn't return the id for the new car_park. I've also noticed that it is not inserting the correct values in the various enumerated columns - see the insert statement. I'm not sure what is happening.Hope that helps a bit.. Regards Alan
Thanks