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

使用leftJoin时, Model的columnMap不工作 #12715

Closed
sprufu opened this issue Mar 16, 2017 · 12 comments
Closed

使用leftJoin时, Model的columnMap不工作 #12715

sprufu opened this issue Mar 16, 2017 · 12 comments
Milestone

Comments

@sprufu
Copy link

sprufu commented Mar 16, 2017

Hi, 在使用php7.0 + phalcon3.0.4时model的属性为null, 以下是使用前提:

  1. model自定义了columnMap().
  2. 调用了QueryBuilder::leftJoin().
class User extend \Phalcon\Mvc\Model {
    public function columnMap() {
        return [
            'user_name' => 'name',
            'user_id' => 'uid'
        ];
    }
}

class UserTest extends \PHPUnit_Framework_TestCase {
    public function testGetNameReturnNullValue() {
        $query = $modelsManager->createBuilder()
            ->andFrom(Order::class, 'o')
            ->leftJoin(User::class, 'o.clientId=u.uid', 'u')
            //->addFrom(User::class, 'u')->andWhere('o.clientId=u.uid')
            ->columns('u.*')
            ->getQuery();
        $users = $query->execute();

        /** @var User $user */
        $user = $users[0];
        self::assertNotNull($user->getName());   // 断言失败, 但使用addFrom代替leftJoin时却成功
    }
}

Details

  • Phalcon version: (php --ri phalcon)
  • PHP Version: (php -v)
  • Operating System:
  • Installation type: Compiling from source || installing via package manager
  • Zephir version (if any):
  • Server: Nginx | Apache | Other
  • Other related info (Database, table schema):
@Jurigag
Copy link
Contributor

Jurigag commented Mar 16, 2017

@dreamsxin any idea what's a problem here?

@dreamsxin
Copy link
Contributor

@Jurigag The following two code returns difference results:
One:

        $query = $modelsManager->createBuilder()
            ->andFrom(Order::class, 'o')
            ->leftJoin(User::class, 'o.clientId=u.uid', 'u')
            ->columns('u.*')->getQuery();
        $users = $query->execute();

Two:

        $query = $modelsManager->createBuilder()
            ->andFrom(Order::class, 'o')
           ->addFrom(User::class, 'u')->andWhere('o.clientId=u.uid')
            ->columns('u.*')->getQuery();
        $users = $query->execute();

@dreamsxin
Copy link
Contributor

dreamsxin commented Mar 16, 2017

Use left join the table u record may be null, you can check all result.
看两者记录条数是否一致 var_dump(count($users));

@sprufu
Copy link
Author

sprufu commented Mar 16, 2017

检查过结果集的,有多条记录,但都不正确。

@dreamsxin
Copy link
Contributor

Left join右表不一定要有数据,所以你可以互换两表位置,你可以直接在数据库里sql查下

@sprufu
Copy link
Author

sprufu commented Mar 17, 2017

dreamsxin, 你好, 在数据库sql查右表有数据, 我仔细检查过了.

@dreamsxin
Copy link
Contributor

@sprufu select u.* from order as o left join user u on o.clientID=u.uid 是这样么

@sprufu
Copy link
Author

sprufu commented Mar 17, 2017

dreamsxin, 你好, 这确实有问题, 关键的地方是自定义了User::columnMap(), 我把能重现问题的代码尽量减少到全面且最少, 你看一下有什么问题, 谢谢

MySQL的数据库:

-- 用的是MySQL

CREATE TABLE `order` (
	`id` INT NOT NULL,
	`clientId` INT NOT NULL,
	PRIMARY KEY (`id`)
);

CREATE TABLE `user` (
	`user_id` INT(11) NOT NULL,
	`user_name` VARCHAR(50) NOT NULL,
	PRIMARY KEY (`user_id`)
);

INSERT INTO `test`.`user` (`user_id`, `user_name`) VALUES (1, 'sprufu');
INSERT INTO `test`.`order` (`id`, `clientId`) VALUES (1, 1);

php相关代码:

class User extends \Phalcon\Mvc\Model {
    protected $name;
    protected $uid;

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

    public function columnMap() {
        return [
            'user_name' => 'name',
            'user_id' => 'uid'
        ];
    }
}

class Order extends \Phalcon\Mvc\Model {
    protected $id;
    protected $clientId;
}

class UserTest extends \PHPUnit_Framework_TestCase {
    private $di;
    protected function setUp() {
        $di = new \Phalcon\Di\FactoryDefault();
        $di->setShared('db', function () {
            $connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
                'host' => '127.0.0.1',
                'port' => 3306,
                'username' => 'test_user',
                'password' => 'test_12345',
                'dbname' => 'test',
                'charset' => 'UTF8'
            ]);
            return $connection;
        });

        $this->di = $di;
    }

    public function testGetNameReturnNullValue() {
        $modelsManager = $this->di->get('modelsManager');
        $query = $modelsManager->createBuilder()
            ->addFrom(Order::class, 'o')
            ->leftJoin(User::class, 'o.clientId=u.uid', 'u')
            ->columns('u.*')
            ->getQuery();
        $users = $query->execute();

        self::assertGreaterThanOrEqual(1, count($users)); // ok

        $user = $users[0];
        self::assertNotNull($user->getName());   // fail
    }
}

@dreamsxin
Copy link
Contributor

dreamsxin commented Mar 17, 2017

我一直以为数据不对,原来是columnmap错了,已经找到问题。

@dreamsxin
Copy link
Contributor

See #12719 你可以测试这个分支

sergeyklay added a commit that referenced this issue Mar 18, 2017
@sergeyklay
Copy link
Contributor

Fixed in the 3.1.x branch.

@sergeyklay sergeyklay modified the milestones: 3.1.x, 3.1.0 Mar 18, 2017
@nottellyou
Copy link

两位高手呀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants