doudi7570 2014-03-03 11:44
浏览 24
已采纳

Laravel 4继承的属性为null

I have a model, which inherits from the Toddish\Verify Laravel package (https://github.com/Toddish/Verify-L4/blob/master/src/Toddish/Verify/Models/User.php)

All I want to do is add some attributes:

use Toddish\Verify\Models\User as VerifyUser;

class User extends VerifyUser
{
    public function __construct (array $attributes = array()) {
        parent::__construct($attributes);
        $this->fillable = array_merge ($this->fillable, array(
            'salutation', 'title', 'firstname', 'lastname', 'phonenumber', 'mobilenumber'
        ));
    }
}

When I run a test:

class UserTest extends TestCase {

    public function testUserCreation () {
        $user = User::create(
            [
                'username' => 'testusername',
                'email' => 'email@test.com',
                'password' => 'testpassword',
                'salutation' => 'MrTest',
                'title' => 'MScTest',
                'firstname' => 'Testfirstname',
                'lastname' => 'Testlastname',
                'phonenumber' => 'testPhoneNumber',
                'mobilenumber' => 'testMobileNumber',
            ]
        );

        $this->assertEquals($user->salutation, 'MrTest');
        $this->assertEquals($user->title, 'MScTest');
        $this->assertEquals($user->firstname, 'Testfirstname');
        $this->assertEquals($user->lastname, 'Testlastname');
        $this->assertEquals($user->phonenumber, 'testPhoneNumber');
        $this->assertEquals($user->mobilenumber, 'testMobileNumber');
    }
}

I get the following:

1) UserTest::testUserCreation
Failed asserting that 'MrTest' matches expected null.

All of the asserts return null. But I checked and the database columns exist. So why is that attribute null?

EDIT:

If I swap the assert parameters:

$this->assertEquals('MrTest', $this->salutation);

I get this:

ErrorException: Undefined property: UserTest::$salutation
  • 写回答

1条回答 默认 最新

  • donglv9116 2014-03-03 11:49
    关注

    You will need to move the fillable override to be above the call to parent::__construct($attributes);

    Making it:

    public function __construct (array $attributes = array()) {
        $this->fillable = array_merge ($this->fillable, array(
            'salutation', 'title', 'firstname', 'lastname', 'phonenumber', 'mobilenumber'
        ));
        parent::__construct($attributes);
    }
    

    This is because the main Model classes constructor uses the fillable array, so it needs to be set before the constructor is called.

    [EDIT] Updated answer to include only the section that is required, and added some explanation of why it is the case

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图