2016-06-28 3 views
2

У меня есть модель (класс php) и таблица в моей базе данных. Когда я пытаюсь выполнить find_many без предложения where, он находит все записи в базе данных. И он создает модель для каждой из записей базы данных. Но эти модели пустые и не имеют назначенных данных, потому что у меня есть собственный сеттер/получатель для моей модели.Как использовать собственный сеттер/приемник с Idiorm/Paris?

<?php 

class City extends Model { 

    private $id; 
    private $idOwm; 
    private $idBla; 
    private $name; 
    private $longitude; 
    private $latitude; 
    private $country; 

    public function data() { 
     return $this->has_many('Data', 'idCity'); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function setId($id) { 
     $this->id = $id; 
    } 

    public function getIdOwm() { 
     return $this->idOwm; 
    } 

    public function setIdOwm($idOwm) { 
     $this->idOwm = $idOwm; 
    } 

    public function getIdBla() { 
     return $this->idBla; 
    } 

    public function setIdBla($idBla) { 
     $this->idBla = $idBla; 
    } 

    public function getName() { 
     return $this->name; 
    } 

    public function setName($name) { 
     $this->name = $name; 
    } 

    public function getLongitude() { 
     return $this->longitude; 
    } 

    public function setLongitude($longitude) { 
     $this->longitude = $longitude; 
    } 

    public function getLatitude() { 
     return $this->latitude; 
    } 

    public function setLatitude($latitude) { 
     $this->latitude = $latitude; 
    } 

    public function getCountry() { 
     return $this->country; 
    } 

    public function setCountry($country) { 
     $this->country = $country; 
    } 
} 

$cities = Model::factory('City')->find_many(); 

$cities представляет собой массив со многими моделями типа City.

City Object 
(
    [id:protected] => 
    [idOwm:protected] => 
    [idBla:protected] => 
    [name:protected] => 
    [longitude:protected] => 
    [latitude:protected] => 
    [country:protected] => 
    [orm] => ORM Object 
     (
      [_connection_name:protected] => default 
      [_table_name:protected] => city 
      [_table_alias:protected] => 
      [_values:protected] => Array 
       (
       ) 

      [_result_columns:protected] => Array 
       (
        [0] => * 
       ) 

      [_using_default_result_columns:protected] => 1 
      [_join_sources:protected] => Array 
       (
       ) 

      [_distinct:protected] => 
      [_is_raw_query:protected] => 
      [_raw_query:protected] => 
      [_raw_parameters:protected] => Array 
       (
       ) 

      [_where_conditions:protected] => Array 
       (
       ) 

      [_limit:protected] => 
      [_offset:protected] => 
      [_order_by:protected] => Array 
       (
       ) 

      [_group_by:protected] => Array 
       (
       ) 

      [_having_conditions:protected] => Array 
       (
       ) 

      [_data:protected] => Array 
       (
        [id] => 1 
        [idOwm] => 2950159 
        [idBla] => 0 
        [name] => Berlin 
        [latitude] => 52.52 
        [longitude] => 13.41 
        [country] => DE 
       ) 

      [_dirty_fields:protected] => Array 
       (
       ) 

      [_expr_fields:protected] => Array 
       (
       ) 

      [_is_new:protected] => 
      [_instance_id_column:protected] => id 
     ) 

) 

Как я могу использовать свой собственный сеттер/геттер с Idiorm/Париж? Возможно ли это, или мне придется делать некоторую модельную логику по-другому?

ответ

0

Как только я удаляю свои личные/защищенные переменные класса в верхней части своих моделей, он работает.

<?php 

class City extends Model { 

    public function data() { 
     return $this->has_many('Data', 'idCity'); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function setId($id) { 
     $this->id = $id; 
    } 

    public function getIdOwm() { 
     return $this->idOwm; 
    } 

    public function setIdOwm($idOwm) { 
     $this->idOwm = $idOwm; 
    } 

    public function getIdBla() { 
     return $this->idBla; 
    } 

    public function setIdBla($idBla) { 
     $this->idBla = $idBla; 
    } 

    public function getName() { 
     return $this->name; 
    } 

    public function setName($name) { 
     $this->name = $name; 
    } 

    public function getLongitude() { 
     return $this->longitude; 
    } 

    public function setLongitude($longitude) { 
     $this->longitude = $longitude; 
    } 

    public function getLatitude() { 
     return $this->latitude; 
    } 

    public function setLatitude($latitude) { 
     $this->latitude = $latitude; 
    } 

    public function getCountry() { 
     return $this->country; 
    } 

    public function setCountry($country) { 
     $this->country = $country; 
    } 
} 

$cities = Model::factory('City')->find_many(); 

Я предполагаю, что это действительно удобно иметь переменные там использовать генерацию кода PhpStorm, но тогда вы должны удалить их.

2

Вы не можете делать это, поскольку Idiorm полагается на магические методы и __get() - если вы включаете в себя правильные свойства класса с тем же именем, то эти магические методы никогда не будут доступны. См. PHP manualdocs link для более подробной информации об этом.

Чтобы получить работу IDE, вы можете использовать PHPDoc @property commentsdocs link, который предназначен для этой цели. Я фактически не использую PHPStorm, но из того, что я вижу в своей документации, он понимает комментарии PHPDoc и может использовать их для понимания структуры кода.

Так что ваш код будет выглядеть следующим образом:

/** 
* @property int $id 
* @property int $idOwm 
* @property int $idBla 
* @property string $name 
* @property string $longitude 
* @property string $latitude 
* @property string $country 
*/ 
class City extends Model { 

    public function data() { 
     return $this->has_many('Data', 'idCity'); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    ... 
} 

Этот другой StackOverflow вопрос и ответ предполагает, что это будет работать в Netbeans: https://stackoverflow.com/a/15160464/461813

Смежные вопросы