SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does this save() function do?

    What does this save() function do? I understand the first array part that it takes variables from a form and puts them in the fields for the Guestbook database.
    But I don't quite follow what happens on the second part beginning with the IF statement. Could somebody explain in human english what it is saying?

    PHP Code:
        public function save(Default_Model_Guestbook $guestbook)
        {
            
    $data = array(
                
    'email'   => $guestbook->getEmail(),
                
    'comment' => $guestbook->getComment(),
                
    'date_created' => date('Y-m-d H:i:s'),
            );

            if (
    null === ($id $guestbook->getId())) {
                unset(
    $data['id']);
                
    $this->getDbTable()->insert($data);
            } else {
                
    $this->getDbTable()->update($data, array('id = ?' => $id));
            }
        } 

    Also if I wanted my update function to add date_updated in my database do I do it like this or do I have to remove this save() function and replace it with the add() and update() functions instead?

    PHP Code:
            } else {
                
    $this->getDbTable()->update($data, array('id = ?' => $id,
                                                         
    'date_created' => date('Y-m-d H:i:s'),
                                                         ));
            } 

  2. #2
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by konsama View Post
    What does this save() function do? I understand the first array part that it takes variables from a form and puts them in the fields for the Guestbook database.
    You are already wrong here. the save function gets passed on an object, $guestbook (I guess you might have to read up on what objects are).

    The first part then creates a new array, which uses the object to retrieve the email and the comment submitted using the form, and adds the date.

    Now you have an array that stores the data at the moment.

    The if statement basically checks whether the comment is a new comment (null === . the variable $id is at the same time populated using another object function to retrieve the id of the comment), if it is null, then we just make sure that the array element id is not set, and only then is the data saved in the database.

    If there is an id, then the user has edited a comment and thus $id = $guestbook->getId() populates the variable $id with the respective id and updates the respective data record in the database.



    Quote Originally Posted by konsama View Post
    Also if I wanted my update function to add date_updated in my database do I do it like this or do I have to remove this save() function and replace it with the add() and update() functions instead?

    PHP Code:
            } else {
                
    $this->getDbTable()->update($data, array('id = ?' => $id,
                                                         
    'date_created' => date('Y-m-d H:i:s'),
                                                         ));
            } 
    This is too much for me at the moment, and I guess we need a bit more code from your script to give you an answer to that.
    Dan G
    Marketing Strategist & Consultant

  3. #3
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll read up on objects like you suggested later. In the meantime I'll post all the source codes for everybody to watch.

    So I'm trying to learn Zend Framework and I'm also trying to figure out how to add an UPDATE and DELETE function for the Zend Quickstart tutorial. But I don't know how?
    http://framework.zend.com/docs/quick...database-table

    Ever since the Zend developers introduced the Data mapping pattern I'm back to baby steps again just when I managed to understand how to work with the old ZF before the 1.8.0 era time.


    These are all the relevant files in play from the Zend Quickstart tutorial.
    I'm working on 2 and 3 at the moment, the GuestbookMapper and Guestbook Model parts. That's where you can find the save() function with some other functions.


    1.
    /Quickstart/application/models/DbTable/Guestbook.php
    http://pastebin.com/m4ae2031d

    2.
    /Quickstart/application/models/GuestbookMapper.php
    http://pastebin.com/m305c9e78

    3.
    /Quickstart/application/models/Guestbook.php
    http://pastebin.com/m3e93ee7d

    4.
    /Quickstart/application/controllers/GuestbookController.php
    http://pastebin.com/m6dabdd12

    5.
    /Quickstart/application/views/scripts/guestbook/index.phtml
    http://pastebin.com/m1acd5b0a

  4. #4
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Too much at the moment for me to handle. Was the bit's and pieces I provided at least helpful to understand the general concept?

    Also, imho, it's better if you know the basics of php/mysql, like what objects are, before you can dive into something as complex as the Zend framework.
    Dan G
    Marketing Strategist & Consultant

  5. #5
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
            if (null === ($id $guestbook->getId())) {
                unset(
    $data['id']);
                
    $this->getDbTable()->insert($data);
            } else {
                
    $this->getDbTable()->update($data, array('id = ?' => $id));
            } 
    Your explanation was helpful but I have a few follow up questions regarding them.

    The if statement basically checks whether the comment is a new comment (null === . the variable $id is at the same time populated using another object function to retrieve the id of the comment),
    Do you have a link to some PHP manual or blog that explains "===" more in detail, it's weird looking. I can't find it on google does it have a special name this "==="?
    • Usually "=" means to save data into a $variable.
    • "==" means to compare 2 things. Wouldn't this work instead of "==="?



    if it is null, then we just make sure that the array element id is not set, and only then is the data saved in the database.

    If there is an id, then the user has edited a comment and thus $id = $guestbook->getId() populates the variable $id with the respective id and updates the respective data record in the database.
    So if someone edits a comment then the date_created field in my database will be overwritten with a new date?

  6. #6
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all now it's starting to make more sense. But I'll have some more questions tomorrow after I figured out how to best formulate my Data pattern question.

    PHP Code:
            if (null === ($id $guestbook->getId())) {
                unset(
    $data['id']);
                
    $this->getDbTable()->insert($data);
            } else { 
    Until tomorrow I have one little question before sleep time. If $id is of type INTEGER and it is empty or doesn't exist (NULL).
    Why is it then necessary to unset the id field in my database before saving the array of Form data? I mean $id is already empty

    ... never mind about this last question I saw the answer 2 post above

  8. #8
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by konsama View Post
    Do you have a link to some PHP manual or blog that explains "===" more in detail, it's weird looking. I can't find it on google does it have a special name this "==="?
    • Usually "=" means to save data into a $variable.
    • "==" means to compare 2 things. Wouldn't this work instead of "==="?
    I guess the link above has clarified this, just to make sure, afaik if $id was set to 0 and you only use null == as a comparison, it would be true, but I'm not 100% on this in this case. It definitely works when you have $id set to 0 and do false == $id, then the statement would be true, whereas if you use false === $id, then the statement would be false.

    Quote Originally Posted by konsama View Post
    So if someone edits a comment then the date_created field in my database will be overwritten with a new date?
    Not in its original form, but with your suggested alteration a couple of posts later, then I guess so, yes. I think to make it work with having a separate date update, you need to have an additional column in your table. But again, I simply don't have the time at the moment to look much further into this.
    Dan G
    Marketing Strategist & Consultant

  9. #9


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •