在奏鸣曲电子商务中创建产品 - 没有可用的对象类型

Posted

技术标签:

【中文标题】在奏鸣曲电子商务中创建产品 - 没有可用的对象类型【英文标题】:creating product in sonata e-commerce - no object types available 【发布时间】:2018-11-03 09:56:15 【问题描述】:

我在创建产品时遇到了 Sonata 电子商务问题。我关注sonata documentation 和其他与此相关的帖子,我不断收到消息“没有可用的对象类型”

我的文件看起来像:

product.yml

services:
    sonata.ecommerce_demo.product.bowl.manager:
        class: Sonata\ProductBundle\Entity\ProductManager
        arguments:
            - Application\Sonata\ProductBundle\Entity\Bowl
            - @doctrine

    sonata.ecommerce_demo.product.bowl.type:
        class: Application\Sonata\ProductBundle\Provider\BowlProductProvider
        arguments:
            - @serializer

奏鸣曲产品.yml

sonata_product:
    products:
        sonata.ecommerce_demo.product.bowl:
            provider: sonata.ecommerce_demo.product.bowl.type
            manager: sonata.ecommerce_demo.product.bowl.manager

Entity.Product.xml

<?xml version="1.0" encoding="UTF-8"?>
<serializer>
    <!-- This file has been generated by the SonataEasyExtendsBundle: https://sonata-project.org/bundles/easy-extends -->
    <class name="Application\Sonata\ProductBundle\Entity\Product" exclusion-policy="all" xml-root-name="_product">
        <discriminator-class value="sonata.ecommerce_demo.product.bowl">Application\Sonata\ProductBundle\Entity\Bowl</discriminator-class>
        <property xml-attribute-map="true" name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search"/>
    </class>
</serializer>

碗.php

<?php
/*
 * This file is part of the <name> project.
 *
 * (c) <yourname> <youremail>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\ProductBundle\Entity;

//use Sonata\ProductBundle\Entity\ProductProduct;

/**
 * This file has been generated by the Sonata product generation command ( https://sonata-project.org/ )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class Bowl extends Product

    /**
     * @var integer $id
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    
        return $this->id;
    

    /**
     * @param int $id
     */
    public function setId($id)
    
        $this->id = $id;
    

产品.php

<?php

namespace Application\Sonata\ProductBundle\Entity;

use Sonata\ProductBundle\Entity\BaseProduct as BaseProduct;

/**
 * This file has been generated by the SonataEasyExtendsBundle.
 *
 * @link https://sonata-project.org/easy-extends
 *
 * References:
 * @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 */
/**
* @ORM\DiscriminatorMap(
 *     product" = "Product"
 * )
*/
abstract class Product extends BaseProduct

    /**
     * @var int $id
     */
    protected $id;

    /**
     * Get id.
     *
     * @return int $id
     */
    public function getId()
    
        return $this->id;
    

composer.json

"require": 
        "php": ">=5.3.9",
        "cocur/slugify": "^2.5",
        "doctrine/doctrine-bundle": "~1.4",
        "doctrine/orm": "^2.4.8",
        "enqueue/amqp-lib": "^0.8.23",
        "friendsofsymfony/rest-bundle": "^2.3",
        "friendsofsymfony/user-bundle": "~1.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "jms/serializer-bundle": "^1.5",
        "kriswallsmith/buzz": "^0.15",
        "liip/monitor-bundle": "^2.6",
        "nelmio/api-doc-bundle": "~2.0",
        "sensio/distribution-bundle": "~4.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "sonata-project/admin-bundle": "3.x-dev",
        "sonata-project/block-bundle": "^3.12",
        "sonata-project/cache": "^1.0.3",
        "sonata-project/cache-bundle": "^2.4",
        "sonata-project/classification-bundle": "^3.6",
        "sonata-project/core-bundle": "^3.9",
        "sonata-project/dashboard-bundle": "^0.2.0",
        "sonata-project/datagrid-bundle": "^2.3",
        "sonata-project/doctrine-orm-admin-bundle": "3.x-dev",
        "sonata-project/easy-extends-bundle": "^2.5",
        "sonata-project/ecommerce": "dev-master",
        "sonata-project/formatter-bundle": "^3.4",
        "sonata-project/google-authenticator": "^2.1",
        "sonata-project/media-bundle": "^3.12",
        "sonata-project/news-bundle": "^3.4",
        "sonata-project/notification-bundle": "^3.5",
        "sonata-project/page-bundle": "^3.8",
        "sonata-project/seo-bundle": "^2.5",
        "sonata-project/user-bundle": "3.x-dev",
        "symfony/monolog-bundle": "^3.0.2",
        "symfony/swiftmailer-bundle": "~2.3,>=2.3.10",
        "symfony/symfony": "2.8.*",
        "twig/twig": "^1.0||^2.0"
    ,

我还在 github 上查看了sonata sandbox,但我找不到我所缺少的东西。

我正在使用 symfony 2.8 和 php 7.2

【问题讨论】:

一些来源 (@Neha) - ***.com/questions/48049617/… - 建议从 Bowl 类中删除 abstract 关键字。在我的示例中,有无抽象关键字没有区别,我总是收到消息没有可用的对象类型。为什么会出现这种情况? 【参考方案1】:

经过大量研究,我终于找到了解决方案。 Sonata 电子商务的开发人员从 ProductAdmin.php 中删除了 getClass() 方法。此类仅在分支 1.x 中可用,在更高的分支中不可用。

原始的 getClass() 方法如下:

/**
 * @return string
 */
public function getClass()

    if ($this->hasSubject()) 
        return get_class($this->getSubject());
     elseif ($class = $this->getProductClass()) 
        return $class;
    
    return parent::getClass();

并且会导致一些错误,所以解决方法是添加稍微修改的类,没有第二个条件。将此添加到 ProductAdmin.php。

/**
 * @return string
 */
public function getClass()

    if ($class = $this->getProductClass()) 
        return $class;
    

    return parent::getClass();

我在sonata ecommerce github找到了解决方案。

【讨论】:

以上是关于在奏鸣曲电子商务中创建产品 - 没有可用的对象类型的主要内容,如果未能解决你的问题,请参考以下文章

使用 Sonata 字段类型在 Controller 中创建表单

在 Sonata symfony 4 中创建管理员用户

如何在奏鸣曲页面中创建页面?

我得到了 FatalErrorException: 试图在奏鸣曲管理员中创建自定义操作

如何查看从电子商务中创建单个产品详细信息的页面

在One2many字段中创建的数据也可用作其他模块上的数据