Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I use SoftDelete and columnMap and useDynamicUpdate, The result is not expected! #13306

Closed
limingxinleo opened this Issue Feb 24, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@limingxinleo
Copy link
Contributor

limingxinleo commented Feb 24, 2018

When I use SoftDelete and columnMap and useDynamicUpdate, The result is not expected!

The Code

<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Model;

$di = new FactoryDefault();

$di->setShared('db', function () {
    $db = new \Phalcon\Db\Adapter\Pdo\Mysql(
        [
            'host' => '127.0.0.1',
            'username' => 'root',
            'dbname' => 'dtm',
            'charset' => 'utf8',
            'options' => [
                PDO::ATTR_CASE => PDO::CASE_NATURAL,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
                PDO::ATTR_STRINGIFY_FETCHES => false,
                PDO::ATTR_EMULATE_PREPARES => false,
            ],
        ]
    );
    return $db;
});

$di->setShared('modelsMetadata', function () {

    $modelsMetadata = new Phalcon\Mvc\Model\Metadata\Files(
        [
            'metaDataDir' => './runtime/',
        ]
    );

    return $modelsMetadata;
});

class Group extends Model
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(column="id", type="integer", length=20, nullable=false)
     */
    public $id;

    /**
     *
     * @var integer
     * @Column(column="user_id", type="integer", length=20, nullable=false)
     */
    public $userId;

    /**
     *
     * @var string
     * @Column(column="name", type="string", length=32, nullable=false)
     */
    public $name;

    /**
     *
     * @var integer
     * @Column(column="is_deleted", type="integer", length=3, nullable=false)
     */
    public $isDeleted;

    /**
     *
     * @var string
     * @Column(column="created_at", type="string", nullable=true)
     */
    public $createdAt;

    /**
     *
     * @var string
     * @Column(column="updated_at", type="string", nullable=true)
     */
    public $updatedAt;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("dtm");
        $this->setSource("group");
        $this->addBehavior(
            new Model\Behavior\SoftDelete(
                [
                    'field' => 'isDeleted',
                    'value' => 1,
                ]
            )
        );

        $this->useDynamicUpdate(true);
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Group[]|Group|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Group|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'group';
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return [
            'id' => 'id',
            'user_id' => 'userId',
            'name' => 'name',
            'is_deleted' => 'isDeleted',
            'created_at' => 'createdAt',
            'updated_at' => 'updatedAt'
        ];
    }
}

$group = new Group();
$group->userId = 1;
$group->name = 'xxx';
$group->save();

try {
    $group->delete();
} catch (Exception $ex) {
    $msg = $ex->getMessage() . ' code:' . $ex->getCode() . ' in ' . $ex->getFile() . ' line ' . $ex->getLine() . PHP_EOL . $ex->getTraceAsString();
    echo $msg;
}

Schema

CREATE TABLE `group` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
  `name` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '组名',
  `is_deleted` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `INDEX_USER_ID` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务组';

Details

Column 'userId' doesn't make part of the column map code:0 in /Users/limx/Applications/limingxinleo/bugs/softDeleteWhenColumnMap.php line 166
#0 [internal function]: Phalcon\Mvc\Model->setSnapshotData(Array, Array)
#1 [internal function]: Phalcon\Mvc\Model\Behavior\SoftDelete->notify('beforeDelete', Object(Group))
#2 [internal function]: Phalcon\Mvc\Model\Manager->notifyEvent('beforeDelete', Object(Group))
#3 [internal function]: Phalcon\Mvc\Model->fireEventCancel('beforeDelete')
#4 /Users/limx/Applications/limingxinleo/bugs/softDeleteWhenColumnMap.php(166): Phalcon\Mvc\Model->delete()
#5 {main}
  • Phalcon version: (3.3.0)
  • PHP Version: (7.1.14)
  • Operating System:
  • Installation type: Compiling from source || installing via package manager
  • Zephir version (if any):
  • Server: Nginx
  • Other related info (Database, table schema): msyql
@virgofx

This comment has been minimized.

Copy link
Member

virgofx commented Feb 25, 2018

I believe this is also the same issue as #13302

@virgofx

This comment has been minimized.

Copy link
Member

virgofx commented Feb 26, 2018

Ping @Jurigag Can you confirm the fix here (also documented in #13302) is good to go.

This does fix the issue.

@virgofx

This comment has been minimized.

Copy link
Member

virgofx commented Mar 10, 2018

Confirmed fixed in 3.3.2

@sergeyklay

This comment has been minimized.

Copy link
Member

sergeyklay commented Mar 10, 2018

@limingxinleo Fixed in the 3.3.x branch. Feel free to open a new issue if the problem appears again. Thank you for contributing.

@sergeyklay sergeyklay closed this Mar 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.