使用其他表中的字段在窗体中添加addCondition()

时间:2019-04-02 16:17:41

标签: atk4

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

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

代替仅搜索“ DESK%”之类的MODEL_NAME的方法,我只想显示与当前记录的FK_OS_ID值相同的FK_OS_ID的SYSPC_MODEL表中存在的FK_MODEL_id值。因此,在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']);
?>

3 个答案:

答案 0 :(得分:0)

请查看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));

答案 1 :(得分:0)

或者,您也可以使用下拉菜单以外的其他方法,这是UI示例:

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

在第一列中选​​择值将在下一列中缩小选择范围。您可以在表单内有一个隐藏字段,可以在其中放置最终值。

答案 2 :(得分:0)


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