SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question General Insert Method on a CRUD class - help on trying to understand

    Hello,

    I'm learning PHP and I have some doubts on this method to generally insert data into DB. The class name is crud and here is the method:

    PHP Code:
    public function dbInsert($table$values)
    {

        
    $this->conn();

        
    $fieldnames array_keys($values[0]);

        
    $size sizeof($fieldnames);
        
        
    $i=1;
        
        
    //construction of the prepared statment
        
    $sql "INSERT INTO $table";

        
    $fields '( ' implode(' ,'$fieldnames) . ' )';

        
    $bound '(:' implode(', :'$fieldnames) . ' )';

        
    $sql .= $fields.' VALUES '.$bound;
        
        
    //prepares statement e saves it on variable $stmt
        
    $stmt $this->db->prepare($sql);

        foreach(
    $values as vals)
        {
            
    $stmt->execute($vals);
        }

    To place values on the DB we do:

    PHP Code:
    $crud = new crud();

    $values = array
            (
            array(
    'animal_name'=>'bruce''animal_type'=>'dingo'),
            array(
    'animal_name'=>'bruce''animal_type'=>'kangaroo'),
            );

    $crud->dbInsert('animals'$values); 




    The doubts:
    1) Names convention question:
    Isn't more correct to call $columname, instead of $fieldname ?

    2) Why do we have this?
    PHP Code:
     $i=

    3) Here:
    PHP Code:
    $fieldnames array_keys($values[0]); 
    -> We are keeping on variable $fieldnames, the key value of the $values array, when this array is on the position 0 ? And what is *actually* the value returned, considering our array?

    PHP Code:
    $values = array
            (
            array(
    'animal_name'=>'bruce''animal_type'=>'dingo'),
            array(
    'animal_name'=>'bruce''animal_type'=>'kangaroo'),
            ); 

    4) Here:
    PHP Code:
    foreach($values as $vals)
        {
            
    $stmt->execute($vals);
        } 
    We are telling that, for each (line/element/index ???) of $values array, the actual value will be "given"(?) to vals, and the pointer goes to the next (line/element/index)... ?

    We then execute the prepared statement, but I don't get what are we passing as a param? I mean, what kind of think does the execute PDO method expects as a param?
    Why $stmt->execute($vals); and not only $stmt->execute(); ?


    Can I please have your help on clarifying those doubts?


    Thanks a lot,
    Márcio

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Does this even work?

    2) looks like a leftover from trying to use numbered parameters instead of named ones

    insert into table ('this', 'that') values ( ? , ? ) ;

  3. #3
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haven't tried yet, I'm trying to understand the code before doing it. The source is from here: http://www.phpro.org/classes/PDO-CRUD.html I believe a reliable one...

    I'm learning, so if you or anyone else, knows a well constructed class for CRUD that I can use, using PDO to use on a Abstract DAO class, I'm avid to learn from it.



    Regards,
    Márcio

  4. #4
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm arriving here:

    1) Column name or placeholdername will be nice variable names I believe. Since on the PDO Prepare sintax, the :something will be used on both: the placeholder name; and the columns name.

    2) $i=1 e o sizeof as stated, were not from this version.

    3) Array key will return this:
    array('animal_name', 'animal_type')

    4) It seems it will do something like: execute(array(animal_name,animal_type)); and this array is what $vals will contain, on each loop occurrence.






    I didn't know that the PDO execute() method could accept arrays as parameters and, normally, I'm used to use bindParams instead, so that I can also have more control by using PDO::PARAM_INT or PDO::PARAM_STR.

    Can anyone help me by given some lights about how can we introduce bindParams here ?

    Thanks a lot,
    Márcio

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are better off just using PDO without this crud class.

    PDO is a very rich object and its fundamental that you know how it works, with all of its quirks without abstracting bits of it away from your control.

    There will be many times when you can build in, or pass in a PDO object to your classes.

    Start with the manual and just make sure you understand:

    How it handles failure to start (exceptions) and how important it is to catch that
    The amount of settings you can make
    The variety of ways of dealing with results
    The variety of ways of binding parameters

    Trying to understand all these fundamentals from inside another class is just not a good idea.

  6. #6
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cups... well...

    I've already started to create my generic CRUD class however, I'm wondering:

    Any of you have already used a DAO design pattern in conjunction with a CRUD generic class?
    Now that I'm trying to create a generic CRUD class on a DAO Design pattern, it seems that it makes no sense at all.

    Is there an advantage on doing this?

    My thought:
    On a "insert to database" scenario for example:
    Without the CRUD generic class: to do an insert, we instantiate a DAO class and then call the insert method. Nothing more. Done.

    With the CRUD generic class: maybe we will have less code on the DAO side but, at the end, to insert a record, we still need to instantiate a DAO and call a insert method SO...

    I see no big advantage on using both...

    Can I have your advice on this please?


    Thanks,
    Márcio
    Last edited by oikram; Jul 14, 2009 at 10:53.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2009
    Posts
    994
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To close this thread I will share my progresses here:

    I have seen no big advantage on using a generic CRUD class, when I already have a DAO pattern and I'm using PDO.

    So I rest my case here.


    Regards,
    Márcio


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
  •