【问题标题】:Adding addCondition() in a Form with fields from other tables在包含其他表字段的表单中添加 addCondition()
【发布时间】:2019-08-24 01:11:18
【问题描述】:

我不知道在 $model->addCondition() 语句中访问“CurrentTable.ForeignKey”或“OtherTable.PrimaryKey”的语法。

这是我的代码片段: $mm = new SYSPC_MODEL($this->app->db,['title_field'=>'MODEL_NAME']); $mm->addCondition('MODEL_NAME', 'LIKE', 'DESK%');

我想只显示 SYSPC_MODEL 表中存在的与当前记录 FK_OS_ID 值相同的 FK_OS_ID 的 FK_MODEL_id 值,而不是简单地搜索“DESK%”之类的 MODEL_NAME。所以在 SQL 中,我们应该有类似: SELECT SYSPC_MODEL.MODEL_NAME WHERE ( DHCP_PC.FK_OS_ID = SYSPC_MODEL.id )

为了更容易理解上下文,我尽可能地减少了我的代码:

<?php
include_once ('../include/config.php');
require '../vendor/autoload.php';

class SYSPC_OS  extends \atk4\data\Model {
    public $table = 'SYSPC_OS';
    function init() {
        parent::init();
        $this->addFields([ ['OS_NAME',         'required'=>true, 'caption'=>'Identifiant d\'OS'],
                            ['OS_DESCRIPTION', 'required'=>true, 'caption'=>'Description d\'OS']
                        ]);
    }
}     // End of class SYSPC_OS

class SYSPC_MODEL  extends \atk4\data\Model {
    public $table = 'SYSPC_MODEL';
    function init() {
        parent::init();
        $this->addFields([ ['MODEL_NAME',     'caption'=>'Nom du modele'],
                            ['MODEL_BASE_RPM', 'caption'=>'Rpm de base']
                        ]);
        $this->hasOne('FK_OS_id',[new SYSPC_OS(),'ui'=>['visible'=>false]])->addField('OS_NAME','OS_NAME');
    }
}     // End of class SYSPC_MODEL

class DHCP_PC  extends \atk4\data\Model {
    public $table = 'DHCP_PC';
    function init() {
        parent::init();
        $this->addFields([  ['PCNAME',      'required'=>true, 'caption'=>'Nom du pc']
                        ]);
        $this->hasOne('FK_OS_ID',['required'=>true,new SYSPC_OS(),'ui'=>['visible'=>false]])->addField('OS_NAME','OS_NAME');
        $this->setOrder('PCNAME','asc');
        $this->hasOne('FK_MODEL_id',['required'=>true,new SYSPC_MODEL(),'ui'=>['visible'=>false]])->addField('MODEL_NAME','MODEL_NAME');
    }
}      // End of class DHCP_PC

class PcForm extends \atk4\ui\Form {
    function setModel($m, $fields = null) {
        $PcWidth = 'three';
        parent::setModel($m, false);
        $gr = $this->addGroup('PC name');
        $gr->addField('PCNAME',['required'=>true,'caption'=>'Nom du pc']);
        $gr = $this->addGroup('OS');
        $mm2 = new SYSPC_OS($this->app->db,['title_field'=>'OS_NAME']);
        $gr->addField('FK_OS_ID',['width'=>$PcWidth],['DropDown'])->setModel($mm2);

        $gr = $this->addGroup('Modèle');
        $mm = new SYSPC_MODEL($this->app->db,['title_field'=>'MODEL_NAME']);
        $mm->addCondition('MODEL_NAME', 'LIKE', 'DESK%');  // Works fine but I would like to display only the FK_MODEL_id values 
                                                            // which exist in the SYSPC_MODEL table for the same FK_OS_ID 
                                                            // than the current record FK_OS_ID value :
                                                            // SELECT SYSPC_MODEL.MODEL_NAME WHERE ( DHCP_PC.FK_OS_ID = SYSPC_MODEL.id )
        $gr->addField('FK_MODEL_id', ['width'=>$PcWidth], ['DropDown'])->setModel($mm);
        return $this->model;
    }
}     // End of class PcForm

$app = new \atk4\ui\App();
$app->title = 'Gestion des PC';
$app->initLayout($app->stickyGET('layout') ?: 'Admin');
$app->db = new \atk4\data\Persistence_SQL(
    "pgsql:host=".$GLOBALS['dbhost'].";dbname=".$GLOBALS['dbname'],
    $GLOBALS['dbuser'],
    $GLOBALS['dbpass']
);
$g = $app->add(['CRUD', 'formDefault'=>new PcForm()]);
$g->setIpp([10, 25, 50, 100]);
$g->setModel(new DHCP_PC($app->db),['PCNAME', 'OS_NAME', 'MODEL_NAME']);
?>

【问题讨论】:

  • 这个例子非常复杂,所以我无法完全理解它,但我的猜测是您希望选择第一个字段的值来限制另一个字段中的选项数量。我会试着找例子。
  • 你完全正确:我尝试在 ATK 中设置一个 SQL 语句,如:SELECT SYSPC_MODEL.MODEL_NAME WHERE ( DHCP_PC.FK_OS_ID = SYSPC_MODEL.id ) 我在文档中搜索,发现“addRelatedEntity”和“relEntity”,但没有找到描述这些命令。
  • 我搜索并找到了“addRelatedEntity”和“relEntity”,但没有找到这些命令的描述。它存在吗?请告诉我我是否走在正确的道路上,或者我是否可以在“addCondition”语句中指定另一个表中的字段。感谢您的帮助

标签: atk4


【解决方案1】:

请查看https://github.com/atk4/ui/pull/551 - 这可能就是您要查找的内容。

此处示例:https://ui.agiletoolkit.org/demos/autocomplete.php

文档:https://agile-ui.readthedocs.io/en/latest/autocomplete.html?highlight=lookup#lookup-field

$form = $app->add(new \atk4\ui\Form(['segment']));
$form->add(['Label', 'Add city', 'top attached'], 'AboveFields');

$l = $form->addField('city',['Lookup']);

// will restraint possible city value in droddown base on country and/or language.
$l->addFilter('country', 'Country');
$l->addFilter('language', 'Lang');

//make sure country and language belong to your model.
$l->setModel(new City($db));

【讨论】:

    【解决方案2】:

    您也可以使用下拉菜单以外的其他内容,这里是 UI 示例:

    https://ui.agiletoolkit.org/demos/multitable.php

    在第一列中选​​择值会缩小下一列中的选项。您可以在表单中设置一个隐藏字段,您可以在其中放置最终值。

    【讨论】:

      【解决方案3】:


      感谢您的支持,但我仍有一些问题。
      问题 1:我找到了“addRelatedEntity”和“relEntity”,但没有找到这些命令的描述。它存在吗?这是我的问题的可能解决方案吗?
      问题 2:是否可以在另一个表中“查找”,如果可以,如何查找?
      问题 3:如果“查找”不是解决方案,如何在模型内部进行连接(在 where 子句中进行过滤)?
      问题4:如果join不是解决方案,是否可以在模型内部使用DSQL?
      问题 5:或者您是否有与 CRUD 关联的 DSQL 示例(在多个表之间进行自制连接)?

      【讨论】:

      • 你的问题很好。你有没有让它工作?您的后续问题也很好,太糟糕了,您将它们发布为答案而不是对实际答案的评论。所以,可能没有人见过他们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多