我正在尝试为撇号创建产品引擎。我在扩展页面设置表单时遇到问题,目前我想添加一个简单的文本区域来向页面添加概要 - 最终我想添加产品设置,但我需要先让基础工作。
我已经创建了一个表单和一个部分设置,它显示正常并保存数据(借助一点技巧 - 可能不正确)。我遇到的麻烦是当您编辑页面时,数据没有被拉回表单。老实说,我可能做了一些根本错误的事情,但我缺乏 Symfony 的经验。
我的表架构
ccProduct:
tableName: cc_product
actAs:
Timestampable: ~
columns:
page_id:
type: integer
notnull: true
synopsis:
type: text
relations:
Page:
class: aPage
local: page_id
foreign: id
type: one
onDelete: CASCADE
我的表格 ccProductEngineForm.class.php
class ccProductEngineForm extends ccProductForm
{
public function __construct($object = null, $options = array(), $CSRFSecret = null)
{
// when editing the page the values are not show, this is an attempt to get it to work - it still doesn't :(
$page_id = sfContext::getInstance()->getRequest()->getParameter('id');
sfContext::getInstance()->getRequest()->setParameter('page_id', $page_id);
$ccProduct = Doctrine::getTable('ccProduct')->findOneByPageId($page_id);
if ($ccProduct) {
sfContext::getInstance()->getRequest()->setParameter('id', $ccProduct->getId());
}
// aPageForm object is passed in
parent::__construct(null, $options, $CSRFSecret); // construct normally
//$this->mergeForm(new aPageForm($object, $options, $CSRFSecret)); // merge the aPageForm - Nope, ignore it!?
}
public function setup() {
parent::setup();
$this->useFields(array('synopsis'));
$this->widgetSchema->setNameFormat('enginesettings[%s]');
$this->widgetSchema->setFormFormatterName('aPageSettings');
}
protected function doSave($con = null)
{
// page_id is missing! possible bug? BaseaActions.class.php ~ 520
$this->values['page_id'] = sfContext::getInstance()->getRequest()->getParameter('enginesettings[pageid]');
parent::doSave($con);
}
}
提前感谢您的帮助
编辑:
感谢您的回答汤姆,我会尝试添加更多细节。
我知道页面对象被传递到引擎中,但我不确定如何处理它 - 请参阅我困惑的代码行:
//$this->mergeForm(new aPageForm($object, $options, $CSRFSecret)); // merge the aPageForm - Nope, ignore it!?
澄清我的“产品”是一个使用 ccProduct 引擎的页面。我现在想向该页面添加额外信息。那有意义吗?用你的话..
您是否正在尝试实际创建一个在产品引擎页面上有其唯一“家”的独特产品?这就是 ccProductForm 的子类化
是的 :)
编辑2:
按照 Tom 的第一个建议(Apostrophe CMS: Engine Creation),我能够使用我的额外字段扩展 aPage 表,并且引擎现在正在保存这些。
但是,标准的 aPageTable::getPagesInfo 函数没有返回我保存的字段。我想我必须单独选择这些?
编辑 3:
aPageTable::retrieveBySlug() 将完成这项工作:)
重访
我决定重新审视这个并尝试汤姆的第二种方法..
另一种方法(如果出于某种原因您不想在 aPage 中添加额外的列)是保留您的 ccProduct 表并获取相关的表
我设法让它工作,我的 ccProductEngine 表单构造函数现在看起来像这样..
class ccProductEngineForm extends ccProductForm
{
public function __construct($aPage = null, $options = array(), $CSRFSecret = null)
{
$page_id = $aPage->getId();
if ($page_id) {
$product = Doctrine_Core::getTable('ccProduct')->findOneByPage_id($page_id);
if ($product) {
$ccProduct = $product;
} else {
$ccProduct = new ccProduct();
}
}
parent::__construct($ccProduct, $options, $CSRFSecret);
}
我希望这可以帮助别人 :)