SachaMorard / phalcon-cassandra

https://phalcon-cassandra.vercel.app
MIT License
10 stars 1 forks source link

Access to undefined property on create() or save() #4

Open mohahn opened 5 years ago

mohahn commented 5 years ago

My model looks like (columns in Cassandra table are named not in camel case but with underscore in name)

<?php
namespace App\Models;

use Phalcon\Mvc\CassandraModel;

/**
 * @Source('dbCassandra', 'myTable')
 */
class MyTable extends CassandraModel {
    /**
     * @Primary
     * @Column(type="text", column="user_id", nullable=false)
     */
    public $userId;

    /**
     * @Column(type="int", column="site_id", nullable=false)
     */
    public $siteId;

    /**
     * @Column(type="text", column="ga_id", nullable=false)
     */
    public $gaId;
}

what works is the following (I get a correct user_id from table)

$model = MyTable::findFirst();
var_dump($model ->userId);

what does not work is

$model  = new MyTable();
$model->userId = 123;
$model->siteId = 123;
$model->gaId = 123;
$model->save();
// or
$model  = new MyTable();
$model->save([
    'userId' => 123,
    'siteId' => 123,
    'gaId' => 123,
]);

this gives me

[Fri, 03 Aug 18 08:37:51 +0000] [ERROR] [1024] [201] Access to undefined property App\Models\MyTable::user_id - /home/www/vendor/sachoo/phalcon-cassandra/Library/Phalcon/Mvc/CassandraModel.php
[Fri, 03 Aug 18 08:37:51 +0000] [ERROR] [1024] [201] Access to undefined property App\Models\MyTable::user_id - /home/www/vendor/sachoo/phalcon-cassandra/Library/Phalcon/Mvc/CassandraModel.php
[Fri, 03 Aug 18 08:37:51 +0000] [ERROR] [8] [324] Undefined index: user_id - /home/www/vendor/sachoo/phalcon-cassandra/Library/Phalcon/Mvc/CassandraModel.php
[Fri, 03 Aug 18 08:37:51 +0000] [ERROR] [8] [324] Undefined index: ga_id - /home/www/vendor/sachoo/phalcon-cassandra/Library/Phalcon/Mvc/CassandraModel.php

I'm able to query data, but not able to store data in Cassandra!

mohahn commented 5 years ago

I tried to manually debug this. On line 324 in Library/Phalcon/Mvc/CassandraModel.php I've inserted a print_r($dataTypes); and get only

Array
(
    [site_id] => 0
)

So the text data type is not recognized. When I change it to varchar in model, some (not all!) errors disappear, but even with create() data is not written to Cassandra.

mohahn commented 5 years ago

After changing some code in _exists() method in above class to

        $columnMap = $metaData->getColumnMap($this);
        $mappedPrimary = $columnMap[$primaryKeys[0]];
        if ($this->{$mappedPrimary} === null) {
            return false;
        }

        $uniqueKey = $this->_uniqueKey;
        if ($uniqueKey === null) {
            $this->_uniqueKey = $primaryKeys[0] . ' = ?';
            $this->_uniqueParams = [$this->{$mappedPrimary}];
            $this->_uniqueTypes = [$bindDataTypes[$primaryKeys[0]]];
        }

I get no error anymore in log, but still no data is sent to Cassandra. BTW: Phalcon is 3.4, Datastax PHP driver is version 1.3.2