Sınıf içindeki nesnelerin dizisi olarak kendini döndüren PHP?

0 Cevap php

Ben bir php web uygulaması oluşturmak için CodeIgniter'ı kullanıyorum, ve ben iyi OO uygulamaları kullanmaya çalışıyorum - düşünce birçok okul orada görünüyor hangi. Ben özellikle bir MySQL tablo ile etkileşim için bir sınıf biography_model var. Bu, veri modeli tablosunda sütunları temsil eden bazı sınıf özellikleri vardır, ama aynı zamanda bu tür $image_url şeklinde tabloda bazı özellikleri not vardır. Sınıf yapıcı işlevi o tablodan bu rekoru getirir ve tablodaki $image_url özelliği olmamak üzere get_biography() yöntemini çağırarak tüm nesne özelliklerini ayarlar isteğe bağlı kayıt kimliği parametresini kabul eder. Bu şekilde ben gitmek için hazır tüm yararlı özelliklere sahip denetleyicisi yeni biography_model nesne örneğini: $bio = new biography_model($id);

Ama biz tablodan kayıtların bir çok satır sonuç kümesini dönen en iyi yaklaşım nedir? Her kayıt için ben de $image_url ayarlamanız gerekir. Ben tablodaki kayıtların listesini sorgulama ve ardından yeni biography_model ($ id) nesnenin her id geçirerek, denetleyicisi bu yapabilirdi. Ama sonra Kontrolör doğrudan modelini atlayarak veritabanı sorgulama bir durum olurdu.

Bunun yerine, ben biography_model içinde biography_model nesneler bir dizi dönmek için seçin.

Example:

    class Biography_model extends Model
    {
        /**
         *  This model manages biography information in the 'biography_content' table.
         *  If a biography ID is passed in when instantiating a new object,
         *  then all class properties are set.
         */
        protected $id;
        protected $person_name;
        protected $title;
        protected $image_file_name;
        protected $image_url;
        protected $biography_text;
        protected $active;

        /**
         * Constructor
         *
         *  If an id is supplied when instantiating a new object, then
         *  all class variables are set for the record.
         */
        public function __construct($person_id = NULL)
        {
            parent::Model();
            if(isset($person_id))
            {
                $this->set_property('id',$person_id);
                $this->get_biography();
            }
        }

        /**
         * Sets supplied property with supplied value.
         */
        public function set_property($property, $value)
        {
            // Set image path if $value is the file name
            if($property == 'image_file_name')
            {
                $this->set_property('image_url',$this->get_bio_img_url($value));
            }

            $this->$property = $value;
        }

        /**
         * Gets requested property value.
         */
        public function get_property($property)
        {
            return $this->$property;
        }

        /**
         *  Returns the biography thumbnail image URL
         */
        public function get_bio_img_url($image_name)
        {
            return $this->config->item('parent_url').'assets/img/biography/'.$image_name;
        }

        /**
         * Get one or more biography entries
         */
        public function get_biography()
        {
            // If the ID is set then set model properties.
            if($this->get_property('id'))
            {
                $this->db->where('id',$this->get_property('id'));
                $query = $this->db->get('biography_content');

                if($query->num_rows() == 1)
                {
                    foreach($query->row() as $key => $value)
                    {
                        $this->set_property($key, $value);
                    }
                }
            }
            // Otherwise return result set of all biographies
            else
            {
                // Get the list of record ID's
                $this->db->select('id');
                $query = $this->db->get('biography_content');

                if ($query->num_rows() > 0)
                {   
                    // New array to return result set
                    $biography_list = array();

                    // For each record, return a new biography_model object
                    foreach($query->result() as $value)
                    {
                        $biography_list[] = new biography_model($value->id);
                    }
                }
                return $biography_list;
            }
        }
    }

    // End of Biography_model Class

Bu çalışıyor. Ama makul bir yaklaşımdır? Diğer daha kabul yöntemleri var mı? Ben iki kez veritabanı sorgulama olduğumu gayet farkındayım, ama bu işlemek için iyi bir yol emin değildi. Tüm öneriler bekliyoruz!

Teşekkürler, Kurt

0 Cevap