【问题标题】:How to use own setter/getter with Idiorm/Paris?如何在 Idiorm/Paris 中使用自己的 setter/getter?
【发布时间】:2016-06-28 08:56:59
【问题描述】:

我的数据库中有一个模型(php 类)和一个表。当我尝试不带 where 子句的 find_many 时,它会找到所有数据库条目。它为每个数据库条目创建一个模型。但是这些模型是空的,并且没有分配数据,因为我有自己的模型设置器/获取器。

<?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/Paris 中使用我自己的 setter/getter?这是可能的还是我必须以不同的方式做一些模型逻辑?

【问题讨论】:

    标签: php mysql activerecord getter-setter idiorm


    【解决方案1】:

    您不能这样做,因为 Idiorm 依赖于 __set()__get() 魔术方法 - 如果您包含同名的正确类属性,那么这些魔术方法将永远不会被访问。有关更多详细信息,请参阅PHP manualdocs link

    为了让您的 IDE 正常工作,您可能可以使用专为此目的设计的 PHPDoc @property commentsdocs link。我实际上并没有使用 PHPStorm,但从我在它的文档中可以看到,它确实理解 PHPDoc cmets,并且可以使用它们来理解代码结构。

    所以你的代码应该是这样的:

    /**
     * @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;
        }
    
        ...
    }
    

    this other stackoverflow question and answer表明这将在Netbeans中工作:https://stackoverflow.com/a/15160464/461813

    【讨论】:

      【解决方案2】:

      只要我删除模型顶部的私有/受保护的类变量,它就会起作用。

      <?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 的代码生成真的很方便,但是你应该删除它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多