#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,003
    Rep Power
    376

    Using Classes and select *


    ok so i have a "superclass" with methods such as:

    find_all();
    find_by_id($id);
    .. etc

    Now I inherit from this such as

    PHP Code:
    class Choice extends Common {
        protected 
    $table_name    "choices";
        protected 
    $db_fields     = array ( 'choice_id','q_id','choice','savy_score','pixel','use_voucher','weighting','status');
        protected 
    $primary_key     = array( 'choice_id' );
        
        public 
    $choice_id;
        public 
    $q_id;
        public 
    $choice;
        public 
    $savy_score;
        public 
    $pixel;
        public 
    $use_voucher;
        public 
    $weighting;
        public 
    $status;

    So the question is when i use find_by_id() or find_all(), i am doing SELECT * which is a bad way I know. the thing is even if i overwrite the global with class specific, In different situations I need different columns? I could then set flags but this is already getting too complicated.

    So what is the best solution of avoiding SELECT *.. I am thinking ahead about the huge overhead
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I think you shouldn't write your own ORM in the first place. Use one of the many libraries like Doctrine or Propel.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,003
    Rep Power
    376
    thanks. In my next revision of my site, I will probably change it to one of those. I cant afford to do it now as I want the site to go live asap (well my managers do)

    i went to propel and their demo/example looked like they were using SELECT * i.e.

    PHP Code:
    <?php
    $book 
    BookQuery::create()->findPK(123); // retrieve a record from a database
    $book->setName('Don\'t be Hax0red!'); // modify. Don't worry about escaping
    $book->save(); // persist the modification to the database

    $books BookQuery::create()  // retrieve all books...
      
    ->filterByPublishYear(2009// ... published in 2009
      
    ->orderByTitle()            // ... ordered by title
      
    ->joinWith('Book.Author')   // ... with their author
      
    ->find();
    foreach(
    $books as $book) {
      echo  
    $book->getAuthor()->getFullName();  
    }

IMN logo majestic logo threadwatch logo seochat tools logo