Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Can someone look at this method and explain why a new listing object is created and added to the collection?

I am doing the Extending OOPHP course and I am going through the code base for the example project and trying to understand it line by line. The repo can be found at: https://github.com/treehouse/extend-oo-php/tree/master/basic-directory-code

In the update method in the collection object where a listing is updated, a new listing object is created and added to the array collection. I just can't understand why, because if you add a new listing object then you should have the old listing and the new one. That doesn't make sense to me, or maybe I am missing something. Here is the method:

  /**
     * Filter incoming dataUpdate listing
     *
     * @param array $data User Data
     *
     * @return integer Indicates the number of records updated
     */
    public function update($data)
    {
        // filter incoming data
        $data = array_filter($data);
        // add a new listing
       // THIS IS THE LINE I AM SO CONFUSED ABOUT:
        $this->addListing($data);
        /* create a sql statement to update the data in the listings table
        using the array keys to bind values */
        $sql = 'UPDATE listings SET ';
        foreach (array_keys($data) as $key) {
            if ($key != 'id') {
                $sql .= " $key = :$key, ";
            }
        }
        $sql = substr($sql, 0, -2);
        $sql .= ' WHERE id = :id';
        // prepare the sql statement
        $statement = $this->db->prepare($sql);
        // try binding parameters and executing the statement
        try {
            $statement->execute($data);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        // set an alert based on whether the update was successful or not
        if ($statement->rowCount() > 0) {
            $this->setAlert(
                'success',
                '<strong>Update listing successful!</strong> ' . $data['title']
            );
        } else {
            $this->setAlert('danger', 'Unable to update listing');
        }
        //return the count of affected rows
        return $statement->rowCount();
    }

I know this is a lot of code to look at but it would be good practice for someone else to help me analyse and understand it. Thanks,

1 Answer

jamesjones21
jamesjones21
9,260 Points

is the extended oop course on here or somewhere else ?