2013-10-26 100 views
-1
Warning: spl_object_hash() expects parameter 1 to be object, string given in 
/var/www/sitetwo/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1367 

我创建MainBlogBu​​ndle Category.phpProduct.php和通过PHP应用程序/控制台命令使用YML映射注解用。使用CRUD操作进行添加/编辑/删除/表演动作后,我尝试添加一个类别,并提交Add category form后,我得到了警告:spl_object_hash()预计参数1是对象

Warning: spl_object_hash() expects parameter 1 to be object, string given in 
/var/www/sitetwo/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1367 

我张贴在GitHub上我的示例代码以下是数据库的脚本。

[email protected]:veerpartap/ProblemSymfony.git

/****************************************************************************/ 

-- phpMyAdmin SQL Dump 
-- version 3.4.10.1deb1 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost 
-- Generation Time: Oct 26, 2013 at 01:56 PM 
-- Server version: 5.5.32 
-- PHP Version: 5.5.3-1+debphp.org~precise+2 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 

-- 
-- Database: `sitetwo` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `category` 
-- 

CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `Company` 
-- 

CREATE TABLE IF NOT EXISTS `Company` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`company_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`address` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`owner_name` varchar(150) COLLATE utf8_unicode_ci NOT NULL, 
`status` tinyint(1) NOT NULL, 
`created` datetime NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `Company` 
-- 

INSERT INTO `Company` (`id`, `company_name`, `address`, `owner_name`, `status`, `created`) VALUES 
(1, 'My First Company', 'Street 5A Sector 85 Chandigarh 1665588', 'Mr. Prateek Kumar', 1, '2013-09-06 00:00:00'), 
(2, 'My Second Private Company', 'Street 34N Sector 89, Chandigarh 165898', 'Mr. Saurabh Shuja', 1, '2013-09-07 00:00:00'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `Post` 
-- 

CREATE TABLE IF NOT EXISTS `Post` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`title` varchar(250) COLLATE utf8_unicode_ci NOT NULL, 
`body` longtext COLLATE utf8_unicode_ci NOT NULL, 
`published` datetime NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `posts` 
-- 

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`title` varchar(250) COLLATE utf8_unicode_ci NOT NULL, 
`body` longtext COLLATE utf8_unicode_ci NOT NULL, 
`published` datetime NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `products` 
-- 

CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`category_id` int(11) DEFAULT NULL, 
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, 
`price` decimal(10,0) NOT NULL, 
`description` longtext COLLATE utf8_unicode_ci NOT NULL, 
PRIMARY KEY (`id`), 
KEY `IDX_B3BA5A5A12469DE2` (`category_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `User` 
-- 

    CREATE TABLE IF NOT EXISTS `User` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(150) NOT NULL, 
    `last_name` varchar(150) NOT NULL, 
    `sex` tinyint(1) DEFAULT NULL, 
    `date_of_birth` datetime DEFAULT NULL, 
    `education` varchar(10) NOT NULL, 
    `mobile` varchar(10) NOT NULL, 
    `email` varchar(100) NOT NULL, 
    `address` varchar(200) NOT NULL, 
    `status` tinyint(1) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 

    -- 
    -- Dumping data for table `User` 
    -- 

    INSERT INTO `User` (`id`, `first_name`, `last_name`, `sex`, `date_of_birth`, `education`, `mobile`, `email`, `address`, `status`) VALUES 
    (1, 'Veerpartap', 'Singh', 1, '2008-11-24 00:00:00', 'MCA', '71505897', '[email protected]', 'hl 99 phase 2 sas nagar mohali', 1), 
    (2, 'Vicky', 'Sharma', 1, '2008-05-09 00:00:00', 'MCA', '88754257', '[email protected]', 'Village Burari, Jila Nawanshar', 1); 

    -- 
    -- Constraints for dumped tables 
    -- 

    -- 
    -- Constraints for table `products` 
    -- 
    ALTER TABLE `products` 
    ADD CONSTRAINT `FK_B3BA5A5A12469DE2` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`); 

/****************************************************************************/ 
+0

请问你能解释一下当你抛出异常时你到底想做什么吗?清除缓存? – nifr

+0

顺便说一句,只是看着你的个人资料中提供的[个人主页](http://veerpartap.com/),发现有一点“错误”。正确的术语是软件**工程师**而不是工程师*或者是我不懂的某种文字游戏? :) – nifr

+0

感谢您的信息并纠正错字错误。 关于这个问题,我发现我必须在类别控制器中包含ArrayCollection类。一旦我添加该行,我看到另一个错误,在产品部分 “__toString()”方法找不到在类型“Main \ BlogBu​​ndle \ Entity \ Category”传递给选择字段的对象。要改为读取自定义getter,请将选项“属性”设置为所需的属性路径。 “ –

回答

0

如上评论指出,这个问题是关于我不包括控制器文件Arrarycollection命名空间。但是在包含名称空间之后,我在添加新产品时遇到了另一个错误。

以下是错误消息: “__toString()”方法未找到Main \ BlogBu​​ndle \ Enity \ Category类型的对象到选择字段。

对于这个错误,我们需要添加一个__toString()方法到您的类别实体。例如:

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

PHP的魔术方法__toString()被用于呈现所述对象的文本表示。在这种情况下,在相关实体的表单中选择类别时将使用类别名称。

0

如果您在类XxxxxType中得到__toString()错误扩展AbstractType 您可以在构建器中添加该定义,如下所示。无需修改您的实体。

$builder 
      ->add('enquete','entity',array('class' => 'AdequatSipBundle:Enquete', 
       'property' => 'Id', 'read_only'=>true)) 
      ->add('produit','entity',array('class' => 'AdequatSipBundle:Produit', 
       'property' => 'Name', 'read_only'=>true)) 
相关问题