cakephp HABTM数据库上传找不到问题
我是 cakePHP 的新手,对 PHP 也相当陌生,我浏览了一些 Youtube 视频来帮助我建立一个基本网站,其中包含所有内容的 CRUD 页面。我现在正在尝试设置用户注册页面以将一堆内容添加到 HABTM 表中,但无法弄清楚出了什么问题。
$this->User->create();
if ($this->User->save($this->data)) {
$lvl = $this->data['User']['level'];
$charids = $this->Kanji->find('list',array('conditions'=>array('grade' <= $lvl,'grade' >= 0)));
foreach ($charids as $charid){
$characterList = array('kanji_id'=>$charid,'user_id'=>$this->User->id, 'level'=>2);
$this->kanjisUsers->save($characterList);
需要澄清一点,该网站是一个学校项目,我希望它能够帮助学习日语,其想法是,您可以输入一串日语文本,它会将其简化到您的水平,因此当您注册时,您可以告诉它你的日语水平是多少(1到9之间的整数)然后它将遍历字符列表(汉字表)并找到所有数字等于或小于你的水平(在汉字表中称为“等级”)然后我希望它添加所有将它们添加到 kanjis_users 表中,并使用 int 2 表示它是已知的(然后我将使用一级字符再次执行此操作,并使用 int 1 保存它们以进行“学习”) 我在代码方面得到了帮助,但不确定它是如何工作的,我已经改变了很多东西,但无法弄清楚出了什么问题,有什么建议吗?
注意:我还看到该模型被称为 kanjis_user.php 和 KanjisUser,我读到模型不应该是复数,但是当我尝试更改它时,一切都崩溃了,这可能是问题的一部分吗? 我也尝试过改变=>与 ->反之亦然,还创建了 $characterList 将其从保存功能中删除,不知道这些是否会影响任何东西,因为它从未起作用...
编辑以响应 api55 的评论:
这是 kanjis_users 的模型关系:
class KanjisUser extends AppModel {
var $name = 'KanjisUser';
//Validation stuff here
var $belongsTo = array(
'Kanji' => array(
'className' => 'Kanji',
'foreignKey' => 'kanji_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
这是汉字模式:
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'kanjis_users',
'foreignKey' => 'kanji_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
这是用户模型:
var $hasAndBelongsToMany = array(
'Kanji' => array(
'className' => 'Kanji',
'joinTable' => 'kanjis_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'kanji_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
这只是标准生成的模型(减去验证)。
为了澄清错误,没有错误,它只是不起作用,它保存了用户,但不向 kanjis_users 表添加任何内容。这是现在的整个寄存器控制器:
function register() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$lvl = $this->data['User']['level'];
//$test = $this->Kanji->find ('list',array('Kanji.grade <='<=$lvl, 'AND'=>array('Kanji.grade >=' >= 1)));
$charids = $this->Kanji->find('list',array('conditions'=>array('grade <=' => $lvl,'grade >=' >= 1)));
//print_r($charids);
//exit();
//$this->kanjisUsers->save(array('kanji_id'=>$charids,'user_id'=>$this->User->id));
//$this->kanjisUsers->saveALL(array('kanji_id'=>$charids,'user_id'=>$this->User->id));
foreach ($charids as $charid){
//echo("<p>Charid: ".$charid." is: </p>");
//var_dump($charid);
$this->kanjisUsers->save(array('kanji_id'=>$charid,'user_id'=>$this->User->id),'level'=> 2);
}
//exit();
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
}
两者似乎都生成一个类似的数组,但是我更改代码有时它会崩溃,有时它会拯救用户,但它永远不会对 kanjis_users 表执行任何操作。
我想要什么:(为了清楚起见,进行了简化:
用户表有:姓名、用户名、密码、用户 ID 等... 汉字表有:每行一个汉字,带有ID,英文,年级(从0到9的整数)等... kanjis_users 具有:ID、user_id、kanji_id、创建(日期)、修改(日期)、级别(int)。
我希望用户在注册时设置一个级别,然后当用户创建时,它将使用 Kanji 表中“等级”介于 1 和用户在符号中设置的级别之间的所有行填充 kanjis_users 表向上形式(称为“级别”)。
所以我上面想做的是在保存用户之后(这个用户在顶部 create() ),然后我测试保存是否成功,然后获取用户在表单中输入的级别,并尝试获取kanjis 表中级别等于或小于该级别的行中的所有字符(注意我不想得到 0 的字符,因为它们是最难的......)并将它们全部添加到 kanjis_users 表中。 (这仅用于注册,当使用系统时,用户可以根据需要添加和删除字符)
我注意到 print_r 从汉字表中获取了所有 12000~ 行,所以我认为过滤器不起作用。 ..
我希望这是有道理的,如果我需要提供任何进一步的信息,请告诉我。
I am new to cakePHP and fairly new to PHP as well, I have gone through some Youtube videos to help me make a basic site set up with CRUD pages for everything. I am now trying to set up the user signup page to add a bunch of stuff to a HABTM table but cant figure out what is going wrong.
$this->User->create();
if ($this->User->save($this->data)) {
$lvl = $this->data['User']['level'];
$charids = $this->Kanji->find('list',array('conditions'=>array('grade' <= $lvl,'grade' >= 0)));
foreach ($charids as $charid){
$characterList = array('kanji_id'=>$charid,'user_id'=>$this->User->id, 'level'=>2);
$this->kanjisUsers->save($characterList);
A bit of clarification, The site is for a school project, I want it to help with learning Japanese and the idea is that you can put a string of Japanese text and it will simplify it to your level so when you sign up you tell it what your level of Japanese is (an int between 1 and 9) then it will go through the character list (kanjis table) and find all with a number equal to or less than your level (called 'grade' in the kanjis table) then I want it to add all these to the kanjis_users table with the int 2 to indicate it is known (then I will do it again with one level up characters and save them with int 1 for 'learning')
I had help with the code and am not sure how it all works, I have been changing lots of things and cant figure out what is wrong, any suggestions?
note: I also saw that the model was called kanjis_user.php and KanjisUser, I read the model should not be plural but when I tried to change it everything crashed, could this be a part of the problem?
I have also tried changing => with -> and vice versa and also created $characterList to remove it from the save function, don't know if these affected anything as it never worked...
Edit in response to comment by api55:
Here is the model relation For kanjis_users:
class KanjisUser extends AppModel {
var $name = 'KanjisUser';
//Validation stuff here
var $belongsTo = array(
'Kanji' => array(
'className' => 'Kanji',
'foreignKey' => 'kanji_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Here is the kanji mode:
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'kanjis_users',
'foreignKey' => 'kanji_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
Here is the user model:
var $hasAndBelongsToMany = array(
'Kanji' => array(
'className' => 'Kanji',
'joinTable' => 'kanjis_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'kanji_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
This is just the standard generated model (minus validation).
For clarification on the error, there is no error, it just doesn't work, It saves the user, but dose not add anything to the kanjis_users table. Here is the whole register controller as it is now:
function register() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$lvl = $this->data['User']['level'];
//$test = $this->Kanji->find ('list',array('Kanji.grade <='<=$lvl, 'AND'=>array('Kanji.grade >=' >= 1)));
$charids = $this->Kanji->find('list',array('conditions'=>array('grade <=' => $lvl,'grade >=' >= 1)));
//print_r($charids);
//exit();
//$this->kanjisUsers->save(array('kanji_id'=>$charids,'user_id'=>$this->User->id));
//$this->kanjisUsers->saveALL(array('kanji_id'=>$charids,'user_id'=>$this->User->id));
foreach ($charids as $charid){
//echo("<p>Charid: ".$charid." is: </p>");
//var_dump($charid);
$this->kanjisUsers->save(array('kanji_id'=>$charid,'user_id'=>$this->User->id),'level'=> 2);
}
//exit();
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
}
Both seem to generate a similar array, but however I change the code sometimes it will crash, sometimes it will save the user, but it will never do anything to the kanjis_users table.
What I want:(simplified for clarity:
User table has: name, username, pw, userID etc...
kanjis table has: a Chinese character per row with ID, English, grade(int from 0 to 9) etc...
kanjis_users has: ID, user_id, kanji_id, created(date), modified(date), level(int).
I want a user to put a level when they sign up and then when the user gets created it will populate the kanjis_users table with all the rows in the Kanji table that have a 'grade' between 1 and the level the user put in the sign up form (called 'level').
So what I am trying to do above is after the user is saved (this user create() at the top), I then test if the save was a success and then get the level the user put in the form, and try to get all the characters from the rows from the kanjis table where the level is equal to or less than that (note I don't want to get ones with 0 as they are the hardest ones...) and add them all to the kanjis_users table. (this is only for sign up, when using the system the user can add and remove characters as they wish)
I noticed with the print_r that it was getting all 12000~ rows from the kanjis table, so I think the filter was not working...
I hope this makes sense, please let me know if I need to put any further info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在寄存器视图中创建一个输入框:
$this->Form->input('Kanji', array('type' => 'hidden'));
然后试试这个:
}
工作原理
$lvl
($this->data['User']['level'];
)$i = 0;
)$this->data['Kanji']['Kanji']
因为如果您在表单中选择了它,它将是默认数据。Create an input box in the register view:
$this->Form->input('Kanji', array('type' => 'hidden'));
Then try this:
}
How it works
$lvl
($this->data['User']['level'];
)$charids = $this->Kanji->find('list',array('conditions'=>array('grade <=' => $lvl,'grade >=' => 1)));
)$i = 0;
)$this->data['Kanji']['Kanji']
as it would be default data if you have selected it in a form.好吧,我看到错误的两件事:D
1)你从查找中得到 12000 多行,因为你的查找条件是错误的:S 并且我给你的评论有拼写错误,
查找必须是这样的:
2)你的数据保存错误:
cookbook 解释说数据pass 需要像这样:
所以你的保存部分应该看起来像这样:
所有这些都在 foreach 中,或者你可以使用 saveAll 方法
Ok, I see wrong 2 things :D
1) You get 12000+ rows from the find beacause your find condition is wrong :S and the one commented that i gave you has typos
the find MUST BE like this:
2) the data in your save is wrong:
The cookbook explains that the data passed needs to be like this :
So your save part should look something like this:
all this inside the foreach or you can do the saveAll approach