无法以Symfony2格式将Propel模型保存到对象

时间:2014-10-08 08:44:04

标签: symfony propel

我试图在Symfony2中使用Propel编辑用户的角色。表单呈现正常,正确检查用户当前具有的角色。但是保存新角色并不起作用。我收到错误:Property role_id does not exist in class Acme\SecurityBundle\Model\User

[1] PropelException: Unable to execute DELETE statement [DELETE FROM `user_role` WHERE (user_role.user_id=:p1 AND user_role.role_id=:p2)] [wrapped: Notice: Array to string conversion in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/connection/DebugPDOStatement.php line 102]
    at n/a
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/util/BasePeer.php line 150

    at BasePeer::doDelete(object(UserRoleQuery), object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUserRolePeer.php line 1305

    at Acme\SecurityBundle\Model\om\BaseUserRolePeer::doDelete(object(UserRoleQuery), object(DebugPDO))
        in  line 

    at call_user_func(array('Acme\SecurityBundle\Model\UserRolePeer', 'doDelete'), object(UserRoleQuery), object(DebugPDO))
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php line 1677

    at ModelCriteria->doDelete(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php line 1655

    at ModelCriteria->delete(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUser.php line 1239

    at Acme\SecurityBundle\Model\om\BaseUser->doSave(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUser.php line 1170

    at Acme\SecurityBundle\Model\om\BaseUser->save()
        in /Users/Stan/Websites/MyProject/src/Acme/AdminBundle/Controller/UserManagementController.php line 20

    at Acme\AdminBundle\Controller\UserManagementController->indexAction(object(Request))
        in  line 

    at call_user_func_array(array(object(UserManagementController), 'indexAction'), array(object(Request)))
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2974

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2936

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 3085

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2335

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /Users/Stan/Websites/MyProject/app_dev.php line 17

    at include('/Users/Stan/Websites/MyProject/app_dev.php')
        in /Users/Stan/Websites/MyProject/web/app_dev.php line 5

[2] Symfony\Component\Debug\Exception\ContextErrorException: Notice: Array to string conversion in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/connection/DebugPDOStatement.php line 102
    at n/a
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/connection/DebugPDOStatement.php line 102

    at Symfony\Component\Debug\ErrorHandler->handle('8', 'Array to string conversion', '/Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/connection/DebugPDOStatement.php', '102', array('input_parameters' => null, 'debug' => array('microtime' => '1412844116.1', 'memory_get_usage' => '18895912', 'memory_get_peak_usage' => '19058688')))
        in  line 

    at PDOStatement->execute(null)
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/connection/DebugPDOStatement.php line 102

    at DebugPDOStatement->execute()
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/util/BasePeer.php line 146

    at BasePeer::doDelete(object(UserRoleQuery), object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUserRolePeer.php line 1305

    at Acme\SecurityBundle\Model\om\BaseUserRolePeer::doDelete(object(UserRoleQuery), object(DebugPDO))
        in  line 

    at call_user_func(array('Acme\SecurityBundle\Model\UserRolePeer', 'doDelete'), object(UserRoleQuery), object(DebugPDO))
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php line 1677

    at ModelCriteria->doDelete(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php line 1655

    at ModelCriteria->delete(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUser.php line 1239

    at Acme\SecurityBundle\Model\om\BaseUser->doSave(object(DebugPDO))
        in /Users/Stan/Websites/MyProject/src/Acme/SecurityBundle/Model/om/BaseUser.php line 1170

    at Acme\SecurityBundle\Model\om\BaseUser->save()
        in /Users/Stan/Websites/MyProject/src/Acme/AdminBundle/Controller/UserManagementController.php line 20

    at Acme\AdminBundle\Controller\UserManagementController->indexAction(object(Request))
        in  line 

    at call_user_func_array(array(object(UserManagementController), 'indexAction'), array(object(Request)))
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2974

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2936

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 3085

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /Users/Stan/Websites/MyProject/app/bootstrap.php.cache line 2335

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /Users/Stan/Websites/MyProject/app_dev.php line 17

    at include('/Users/Stan/Websites/MyProject/app_dev.php')
        in /Users/Stan/Websites/MyProject/web/app_dev.php line 5

这是我的架构:

<?xml version="1.0" encoding="UTF-8"?>
<database name="system" namespace="Acme\SecurityBundle\Model" defaultIdMethod="native">
    <table name="user">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="username" required="true" type="varchar" primaryKey="true" size="50" />
    </table>

    <table name="role">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="role" required="true" type="varchar" size="20" primaryString="true" />
        <column name="name" required="true" type="varchar" size="50" />

        <unique>
            <unique-column name="role" />
            <unique-column name="name" />
        </unique>
    </table>

    <table name="user_role" isCrossRef="true">
        <column name="user_id" type="integer" primaryKey="true"/>
        <column name="role_id" type="integer" primaryKey="true"/>
        <foreign-key foreignTable="user">
            <reference local="user_id" foreign="id"/>
        </foreign-key>
        <foreign-key foreignTable="role">
            <reference local="role_id" foreign="id"/>
        </foreign-key>
    </table>
</database>

这就是我在控制器中渲染表单的方式:

class UserManagementController extends Controller
{
    public function indexAction(Request $request)
    {
        $user = UserQuery::create()->filterById(2)->findOne();
        $form = $this->createForm(new UserType($user), $user);
        $form->handleRequest($request);

        if ($form->isValid())
        {
            $user->save();
        }

        return $this->render('AcmeAdminBundle:UserManagement:index.html.twig', array(
            'form' => $form->createView()
        ));
    }
}

这是UserType:

class UserType extends AbstractType
{
    private $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('roles', 'model', array(
                    'class' => 'Acme\SecurityBundle\Model\Role',
                    'multiple' => true,
                    'expanded' => true,
                    'property' => 'name',
                    'label' => $this->user->getUsername(),

                )
            )
            ->add('save', 'submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => 'Acme\SecurityBundle\Model\User',
            ));
    }


    public function getName()
    {
        return 'user';
    }
}

此错误消息来自何处以及如何解决?

1 个答案:

答案 0 :(得分:2)

感谢PropelBundle community我终于明白了:两个主键触发了这个错误。现在我只有一个主键,它可以完美运行。