简体   繁体   English

atk4先进的crud?

[英]atk4 advanced crud?

I have the following tables: 我有以下表格:

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

-- Table `product`

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

CREATE  TABLE IF NOT EXISTS `product` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `productName` VARCHAR(255) NULL ,
  `s7location` VARCHAR(255) NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


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

-- Table `pages`

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

CREATE  TABLE IF NOT EXISTS `pages` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `productID` INT NULL ,
  `pageName` VARCHAR(255) NOT NULL ,
  `isBlank` TINYINT(1) NULL ,
  `pageOrder` INT(11) NULL ,
  `s7page` INT(11) NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `productID` (`productID` ASC) ,
  CONSTRAINT `productID`
    FOREIGN KEY (`productID` )
    REFERENCES `product` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


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

-- Table `field`

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

CREATE  TABLE IF NOT EXISTS `field` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `pagesID` INT NULL ,
  `fieldName` VARCHAR(255) NOT NULL ,
  `fieldType` VARCHAR(255) NOT NULL ,
  `fieldDefaultValue` VARCHAR(255) NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `id` (`pagesID` ASC) ,
  CONSTRAINT `pagesID`
    FOREIGN KEY (`pagesID` )
    REFERENCES `pages` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

I have gotten CRUD to work on the 'product' table. 我已经让CRUD在'产品'表上工作了。

//addproduct.php
class page_addproduct extends Page {
    function init(){
        parent::init();

        $crud=$this->add('CRUD')->setModel('Product');

    }
}

This works. 这有效。 but I need to get it so that when a new product is created it basically allows me to add new rows into the pages and field tables. 但我需要得到它,以便在创建新产品时,它基本上允许我在页面和字段表中添加新行。

For example, the products in the tables are a print product(like a greeting card) that has multiple pages to render. 例如,表中的产品是具有多个要呈现的页面的打印产品(如贺卡)。 Page 1 may have 2 text fields that can be customized, page 2 may have 3 text fields, a slider to define text size, and a drop down list to pick a color, and page 3 may have five text fields that can all be customized. 第1页可以有2个可以自定义的文本字段,第2页可以有3个文本字段,用于定义文本大小的滑块和用于选择颜色的下拉列表,第3页可以有5个可以自定义的文本字段。 All three pages (and all form elements, 12 in this example) are associated with 1 product. 所有三个页面(以及所有表单元素,本例中为12个)都与1个产品相关联。

So when I create the product, could i add a button to create a page for that product, then within the page i can add a button to add a new form element field? 因此,当我创建产品时,我是否可以添加一个按钮来为该产品创建页面,然后在页面中我可以添加一个按钮来添加新的表单元素字段?

I'm still somewhat new to this, so my db structure may not be ideal. 我还是有些新手,所以我的数据库结构可能不太理想。 i'd appreciate any suggestions and feedback! 我很感激任何建议和反馈! Could someone point me toward some information, tutorials, documentation, ideas, suggestions, on how I can implement this? 有人能指出我的一些信息,教程,文档,想法,建议,我如何实现这一点?

Normally you can't add page unless you have an existing product. 通常,除非您有现有产品,否则无法添加页面。 We normally solve that problem with a state field. 我们通常用状态字段来解决这个问题。 Add this into your product model: 将其添加到您的产品型号中:

$this->addField('state')->enum(array('active','draft'))->defaultValue('draft'); 

also create it in SQL. 也可以在SQL中创建它。 Also it's generally good to describe relations, so add this into your product model: 描述关系通常也很好,所以将它添加到您的产品模型中:

$this->hasMany('Page','productID');

Next you will need to change the action when a new record is added. 接下来,您需要在添加新记录时更改操作。 You can do that easily by extending crud class: 你可以通过扩展crud类来轻松地做到这一点:

class MyCRUD extends CRUD {
    function formSubmitSuccess(){
        if($_GET['id']) return parent::formSubmitSuccess(); // edit ->save

        // add ->save handled here
        $this->js()->univ()->location($this->api->url('./details',
          array('id'=>$this->form->model->id)))->execute();
    }
}

and replacing add('CRUD') with add('MyCRUD'). 并用add('MyCRUD')替换add('CRUD')。 Next if you are placing CRUD on page "mypage" you will need to create "mypage/details". 接下来,如果您将CRUD放在页面“mypage”上,则需要创建“mypage / details”。 After successfully saving new product, your new method will redirect user to this new page and it will pass along the productID too. 成功保存新产品后,您的新方法会将用户重定向到此新页面,它也会传递productID。

On this new page, you should have 在这个新页面上,你应该有

$m=$this->add('Model_Product')->load($_GET['id']); // makes sure ID is valid
$this->api->stickyGET('id');  // configures page to carry id= value along
$crud=$this->add('CRUD');
$crud->setModel($m->ref('Page'));

This will display CRUD here which will automatically edit pages of that particular product. 这将在此处显示CRUD,它将自动编辑该特定产品的页面。 The only thing you miss now is a back button. 你现在唯一想念的是一个后退按钮。

if($crud->grid){
    if($crud->grid->addButton('Save')->isClicked()){
        // AJAX action on button click

        $m->set('status','active')->save(); // update status of current product
        $this->js()->univ()->location($this->api->url('..'))->execute();
        // redirect back to parent page
    }

    // You might want a cancel button too
    $crud->grid->addButton('Cacel')->js('click')
         ->univ()->location($this->api->url('..'));

    // no AJAX, simple javascript action.
}

To finish everything nicely, go back to your original page and add a condition. 要完美地完成所有操作,请返回原始页面并添加条件。 You don't have to create new model, unless you want, can be like this: 您不必创建新模型,除非您想要,可以像这样:

// on my page
$this->add('MyCRUD')->setModel('Product')->addCondition('state','active');

This way your main CRUD won't display records which are incomplete. 这样您的主CRUD就不会显示不完整的记录。 Your database may accumulate draft records which you can delete once in a while. 您的数据库可能会累积草稿记录,您可以偶尔删除它们。 If anything here does not work, publish your project on Github and share it with our Agile Toolkit development group, some of us would certainly implement this for you. 如果这里的任何内容不起作用,请在Github上发布您的项目并与我们的Agile Toolkit开发小组共享,我们中的一些人肯定会为您实现这一点。

BONUS: 奖金:

You might want how should you edit existing product pages? 您可能想要如何编辑现有产品页面? Easiest would be to have expander in the main crud's grid: 最容易的是在主要网格中有扩展器:

$crud=$this->add('MyCRUD');
$crud->setModel('Product')->addCondition('state','active');
$crud->addColumn('expander','details'); // will expand into page mypage/details and will automatically pass ID.

This solution is not 100% ideal, but given the character of the WEB applications and their reliance on SQL that's a great way to manage your data. 这个解决方案并非100%理想,但考虑到WEB应用程序的特性以及它们对SQL的依赖,这是管理数据的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM