Objects saving and restoring with MySQL

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

    Objects saving and restoring with MySQL

    If you had a class user with variables id, name, password. How would you
    save this object or its variable date to a MySQL database? And then if
    you had a dbase populated with id, name, password and you wanted to show
    a list of all of these how would you iterate through this and re
    populate the object variables?

    Are there any tutorials that you can recommend on this subject?



    --
    Gav

  • Peter Fox

    #2
    Re: Objects saving and restoring with MySQL

    Following on from Gav's message. . .[color=blue]
    >If you had a class user with variables id, name, password. How would you
    >save this object or its variable date to a MySQL database? And then if
    >you had a dbase populated with id, name, password and you wanted to show
    >a list of all of these how would you iterate through this and re
    >populate the object variables?
    >
    >Are there any tutorials that you can recommend on this subject?
    >
    >
    >[/color]

    You would encapsulate the mechanism in the object as far as possible in
    your environment. For example if your DB is [wrapped in] an object then
    you might have the following as a method of myObject
    function SaveToDb($Datab ase){
    // make some SQL with just the data fields you want saved
    $sql = "update atable set password=$passw ord ..... where
    name=$this->name";
    $Database->ExecuteSQL($so meSQL);
    }

    OR
    function SaveToDb($Datab ase){
    $blob = serialise($this );
    // now save blob in database keyed by name
    $sql = "update atable set blobfield='$blo b' where name=$this->name";
    $Database->ExecuteSQL($so meSQL);
    }

    You will see that in the first example you would have the fields
    explicit in the table but in the second everything but the key is
    blobbed. The second approach has two advantages and one disadvantage
    (but you can mingle)
    + If you change object design you don't need to change the database
    + You can store all sorts of object in the same table
    - You can't SELECT on blobbed data

    For iterating through a database results set or record see the docs it
    is very straight forward EXCEPT for a gotcha in foreach where A COPY of
    objects is worked on not the original. i.e. don't use foreach where
    you'll be modifying data within it.

    * The code above is off the top of my head and will have bugs.




    --
    PETER FOX Not the same since the bookshop idea was shelved
    peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
    2 Tees Close, Witham, Essex.
    Gravity beer in Essex <http://www.eminent.dem on.co.uk>

    Comment

    • NC

      #3
      Re: Objects saving and restoring with MySQL

      Gav wrote:[color=blue]
      >
      > If you had a class user with variables id, name, password. How
      > would you save this object or its variable date to a MySQL
      > database?[/color]

      By using serialize($your _object) and writing the result into a TEXT or
      VARCHAR field.
      [color=blue]
      > Are there any tutorials that you can recommend on this subject?[/color]

      The Manual is always a good place to start:

      Generates a storable representation of a value


      Cheers,
      NC

      Comment

      • Dikkie Dik

        #4
        Re: Objects saving and restoring with MySQL

        > Are there any tutorials that you can recommend on this subject?
        Wrote this when I was searching for any methods myself:



        Hope it is of any help.

        Comment

        • David  Wahler

          #5
          Re: Objects saving and restoring with MySQL

          NC wrote:[color=blue]
          > Gav wrote:[color=green]
          > >
          > > If you had a class user with variables id, name, password. How
          > > would you save this object or its variable date to a MySQL
          > > database?[/color]
          >
          > By using serialize($your _object) and writing the result into a TEXT or
          > VARCHAR field.[/color]

          I don't see how that's better than having separate database fields for
          each object member. It makes it very difficult to efficiently query the
          database based on different criteria, or to migrate to any language
          besides PHP.

          -- David

          Comment

          • Peter Fox

            #6
            Re: Objects saving and restoring with MySQL

            Following on from David Wahler's message. . .[color=blue]
            >NC wrote:[color=green]
            >> Gav wrote:[color=darkred]
            >> >
            >> > If you had a class user with variables id, name, password. How
            >> > would you save this object or its variable date to a MySQL
            >> > database?[/color]
            >>
            >> By using serialize($your _object) and writing the result into a TEXT or
            >> VARCHAR field.[/color]
            >
            >I don't see how that's better than having separate database fields for
            >each object member. It makes it very difficult to efficiently query the
            >database based on different criteria, or to migrate to any language
            >besides PHP.
            >
            >-- David
            >[/color]
            What you do is set up a table with fields something like
            uID,uStatus,uNa me,uData
            where data is blob/text.

            You can select on some items of data but you have complete flexibility
            in the blob when you serialise the object. Suppose for example the user
            object contains a variable size array. Mapping that to table fields
            would be a pain or setting up another table for 1 user to many array
            elements complete overkill when the information isn't going to be used
            in any other context. For example an array of passwords used in last
            three months by this user to prevent immediate re-use.[1]



            [1] Don't try this at home kids! Never store passwords.
            --
            PETER FOX Not the same since the porcelain business went down the pan
            peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
            2 Tees Close, Witham, Essex.
            Gravity beer in Essex <http://www.eminent.dem on.co.uk>

            Comment

            • Jim Michaels

              #7
              Re: Objects saving and restoring with MySQL

              serialize() puts out binary data. VARCHAR and TEXT are made for character
              data. use BLOB(), TINYBLOB, or MEDIUMBLOB.

              "NC" <nc@iname.com > wrote in message
              news:1137004839 .342320.193870@ f14g2000cwb.goo glegroups.com.. .[color=blue]
              > Gav wrote:[color=green]
              >>
              >> If you had a class user with variables id, name, password. How
              >> would you save this object or its variable date to a MySQL
              >> database?[/color]
              >
              > By using serialize($your _object) and writing the result into a TEXT or
              > VARCHAR field.
              >[color=green]
              >> Are there any tutorials that you can recommend on this subject?[/color]
              >
              > The Manual is always a good place to start:
              >
              > http://www.php.net/serialize
              >
              > Cheers,
              > NC
              >[/color]


              Comment

              • Peter Fox

                #8
                Re: Objects saving and restoring with MySQL

                Following on from Gav's message. . .[color=blue]
                >If you had a class user with variables id, name, password. How would you
                >save this object or its variable date to a MySQL database? And then if
                >you had a dbase populated with id, name, password and you wanted to show
                >a list of all of these how would you iterate through this and re
                >populate the object variables?
                >
                >Are there any tutorials that you can recommend on this subject?
                >
                >
                >[/color]

                1 - Serialize into a blob AND use some key fields as required is one
                way. But users tend not to have really complex data footprints so write
                a method for your user class SaveMe() which does the dirty deed field by
                field.

                2 - Or download this very thing from my web site

                [with added access permissions goodness]

                --
                PETER FOX Not the same since the cardboard box company folded
                peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
                2 Tees Close, Witham, Essex.
                Gravity beer in Essex <http://www.eminent.dem on.co.uk>

                Comment

                Working...