Changing data in Objectified mysql records

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerrygarciuh

    Changing data in Objectified mysql records

    Hello,

    I am using this very slightly modified function found here:

    to make mySQL rows into objects of type $classname:

    // This takes db result rows and makes real objects of $classname type
    function &buildObj($resu lt, $classname) {
    while($row = mysql_fetch_ass oc($result)) {
    if ($row === null) return null;

    /* Create the object */
    $obj =& new $classname;

    /* Explode the array and set the objects's instance data */
    foreach($row as $key => $value)
    {
    $obj->{$key} = $value;
    }
    $objs[] = $obj;
    }
    return $objs;
    }

    This is called by a retrieve: function:

    function retrieve($where ) {
    echo $query = "SELECT * FROM $this->table WHERE " . join(' AND ',
    $where);
    $result = mysql_query($qu ery);
    $rows =& buildObj($resul t, get_class($this ));
    mysql_free_resu lt($result);
    return $rows;
    }

    The problem I am encountering is that if I do this in my script:

    $listing = new Listing;
    $listings = $listing->retrieve(array ('row > 1') );
    foreach ($listing as $aListing) {
    $aListing->address='xxxxx xxxxxxxx';
    }
    foreach ($listing as $aListing) {
    print_r($aListi ng);
    }

    The value in $aListing->address is unchanged.

    However if I do this:

    $listing = new Listing;
    $listings = $listing->retrieve(array ('row > 1') );
    foreach ($listing as $aListing) {
    $aListing->address='xxxxx xxxxxxxx';
    $newArray[] = $aListing;
    }
    foreach ($newArray as $aListing) {
    print_r($aListi ng);
    }


    I see the update values in $aListing->address .

    What gives? Can anyone help me out here?

    BTW I also tried removing the reference operator (&) from the whole thing
    but it didn't help, I don't know why the author of the sub had used it.

    TIA,

    jg




  • jerrygarciuh

    #2
    Re: Changing data in Objectified mysql records

    My error was trying to act on $this when I had passed $obj.

    jg


    "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
    news:p4yKc.2819 $fB4.622@lakere ad01...[color=blue]
    > Hello,
    >
    > I am using this very slightly modified function found here:
    > http://us2.php.net/mysql_fetch_object
    > to make mySQL rows into objects of type $classname:
    >
    > // This takes db result rows and makes real objects of $classname type
    > function &buildObj($resu lt, $classname) {
    > while($row = mysql_fetch_ass oc($result)) {
    > if ($row === null) return null;
    >
    > /* Create the object */
    > $obj =& new $classname;
    >
    > /* Explode the array and set the objects's instance data */
    > foreach($row as $key => $value)
    > {
    > $obj->{$key} = $value;
    > }
    > $objs[] = $obj;
    > }
    > return $objs;
    > }
    >
    > This is called by a retrieve: function:
    >
    > function retrieve($where ) {
    > echo $query = "SELECT * FROM $this->table WHERE " . join(' AND ',
    > $where);
    > $result = mysql_query($qu ery);
    > $rows =& buildObj($resul t, get_class($this ));
    > mysql_free_resu lt($result);
    > return $rows;
    > }
    >
    > The problem I am encountering is that if I do this in my script:
    >
    > $listing = new Listing;
    > $listings = $listing->retrieve(array ('row > 1') );
    > foreach ($listing as $aListing) {
    > $aListing->address='xxxxx xxxxxxxx';
    > }
    > foreach ($listing as $aListing) {
    > print_r($aListi ng);
    > }
    >
    > The value in $aListing->address is unchanged.
    >
    > However if I do this:
    >
    > $listing = new Listing;
    > $listings = $listing->retrieve(array ('row > 1') );
    > foreach ($listing as $aListing) {
    > $aListing->address='xxxxx xxxxxxxx';
    > $newArray[] = $aListing;
    > }
    > foreach ($newArray as $aListing) {
    > print_r($aListi ng);
    > }
    >
    >
    > I see the update values in $aListing->address .
    >
    > What gives? Can anyone help me out here?
    >
    > BTW I also tried removing the reference operator (&) from the whole thing
    > but it didn't help, I don't know why the author of the sub had used it.
    >
    > TIA,
    >
    > jg
    >
    >
    >
    >[/color]


    Comment

    Working...