Jurik
Jurik

Reputation: 3264

How to use own setter/getter with Idiorm/Paris?

I have a model (php class) and a table in my database. When I try to find_many without a where clause, it finds all database entries. And it creates a model for each of the database entries. But these models are empty and have no data assigned because I have my own setter/getter for my model.

<?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 is an array with many models of type 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
        )

)

How can I use my own setter/getter with Idiorm/Paris? Is this possible or will I have to do some model logic in a different way?

Upvotes: 2

Views: 248

Answers (2)

Treffynnon
Treffynnon

Reputation: 21553

You can't do this as Idiorm relies on the __set() and __get() magic methods - if you include proper class properties of the same name then those magic methods will never be accessed. See the PHP manualdocs link for more detail on this.

To get your IDE working you can probably use PHPDoc @property commentsdocs link, which is designed for this purpose. I don't actually use PHPStorm, but from what I can see in its documentation it does understand PHPDoc comments and can use them for understanding code structure.

So your code would look like this:

/**
 * @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 suggests that this will work in Netbeans: https://stackoverflow.com/a/15160464/461813

Upvotes: 2

Jurik
Jurik

Reputation: 3264

As soon as I remove my private/protected class variables at the top of my models it works.

<?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();

I guess it is really handy to have the variables there to use code generation of PHPStorm, but then you should delete them.

Upvotes: 0

Related Questions