我有简单的关系表:每个表(协议(protocol))'umowas' 都有表(成人)'rodzics' 中的一项和表( child )'dziecis' 中的一项。
当调用 myaddress.com/cakephp/Umowas/add 时,我没有看到项目“rodzic_id”和“dzieci_id”及其项目名称的下拉菜单。我只看到数字。
例如,“rodzic_id”的下拉菜单显示数组(1=>1, 2=>2, 3=>3),而不是其记录的名称,例如数组( 1=>'项目名称 1', 2 => '项目名称2').....
Debug.log 和 Error.log 为空。
我在屏幕底部的查询是:
Nr 受影响的查询错误数量花费的行数(毫秒)
1 选择
Rodzic.
idFROM
akademia-uat.
rodzicsAS
RodzicWHERE 1 = 1 2 2 3
2 选择
Dzieci.
idFROM
akademia-uat.
dziecisAS
DzieciWHERE 1 = 1 1 1 2
我的代码是:
<?php
class UmowasController extends AppController {
var $name = 'Umowas';
var $scaffold;
var $helpers = array('Form' );
function index() {
$this->Umowa->recursive = 1;
$umowas = $this->Umowa->find('all');
$this->set('umowas', $umowas);
}
}
?>
-----------------------------------------
<?php
class RodzicsController extends AppController {
var $name = 'Rodzics';
var $scaffold;
function index() {
$this->Rodzic->recursive = 1;
$rodzics = $this->Rodzic->find('all');
$this->set('rodzics', $rodzics);
}
}
-------------------------------------------------------
<?php
class DziecisController extends AppController {
var $name = 'Dziecis';
var $scaffold;
function index() {
$this->Dzieci->recursive = 1;
$dziecis = $this->Dzieci->find('all');
$this->set('dziecis', $dziecis);
}
}
------------------------------------------
<?php
class Umowa extends AppModel {
var $name = 'Umowa';
public $belongsTo = array(
'Rodzic' => array(
'className' => 'Rodzic',
'conditions' => '',
'order' => '',
'foreignKey' => 'rodzic_id' //or your external key
),
'Dzieci' => array(
'className' => 'Dzieci',
'conditions' => '',
'order' => '',
'foreignKey' => 'dzieci_id' //or your external key
)
);
var $helpers = array('Form' );
function add() {
if (!empty($this->data)) {
$this->Umowa->create();
$this->Umowa->save($this->data);
$this->redirect(array('action'=>'index'));
}
$rodzics = $this->Umowa->Rodzic->generateList();
$this->set('rodzics', $rodzics);
$dziecis = $this->Umowa->Dzieci->generateList();
$this->set('dziecis', $dziecis);
}
}
?>
--------------------------------------------------------
<?php
class Rodzic extends AppModel
{
var $name = 'Rodzic';
var $hasMany = 'Umowa';
}
?>
-------------------------------------------------------
<?php
class Dzieci extends AppModel
{
var $name = 'Dzieci';
var $hasMany = 'Umowa';
}
?>
---------------------------------------------------------
view/Rodzics/add and do not exists
view/Umowas/add do not exists
view/Dziecis/add do not exist
`
我的表格是这样的:
`
CREATE TABLE IF NOT EXISTS `dziecis` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`imie_dziecka` varchar(18) DEFAULT NULL,
`nazwisko_dziecka` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
CREATE TABLE IF NOT EXISTS `rodzics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`imie_rodzica` varchar(18) DEFAULT NULL,
`nazwisko_rodzica` varchar(15) DEFAULT NULL,
`telefon` int(9) DEFAULT NULL,
`adres_email` varchar(50) DEFAULT NULL,
`ulica` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `umowas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`warunek_umowy` varchar(2048) DEFAULT NULL,
`data_rozwiazania_warunku` date DEFAULT NULL,
`data_zawarcia_umowy` date DEFAULT NULL,
`data_zerwania_umowy` date DEFAULT NULL,
`nr_umowy` int(4) DEFAULT NULL,
`rok_szkolny` int(4) DEFAULT NULL,
`wizerunek` varchar(3) DEFAULT NULL,
`miesiac_rozpoczecia_platnosci` int(2) DEFAULT NULL,
`raty` varchar(3) NOT NULL,
`utworzono` date NOT NULL,
`modyfikacja` date NOT NULL,
`zabezpieczona` varchar(3) NOT NULL,
`wydrukowana` varchar(3) NOT NULL,
`dzieci_id` int(11) NOT NULL,
`rodzic_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`status_umowy_id` int(11) NOT NULL,
`miasto_id` int(11) NOT NULL,
`klasa_lub_grupa_dziecka_id` int(11) NOT NULL,
`cennik_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
最佳答案
我已将项目中的所有表格和名称翻译成英文。还完成了命令行蛋糕烘焙工作,所有表格都开始工作并显示字段。问题出在模型中,并且没有声明为 public $displayField。 每个人都必须做脚手架命令行工具并有英文名称,这很容易。最重要的是声明 $displayField 并将其指向每个表中的标题或名称字段。 感谢大家的支持。
关于php - CakePHP 表关系 $belongsTo 在添加操作中的下拉菜单中不显示任何记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24230342/