我正在玩CakePHP框架及其烘焙方法。所以,首先我创建了一个数据库,然后我开始烘焙 - 结果几乎完美。
所以,我有3个表:users,projects和project_helper。 前两个表是标准表,即:
用户:
用户:
问题是一个项目可以属于许多用户,每个用户可以有很多项目。为了避免制作新栏目"项目"在用户表中并在字符串(2,6,11,77)中保存项目id并在以后将它们扩展到数组中我决定创建另一个包含两个主键(两列)的表。
Project_helper:
它有点工作,我可以列出属于指定用户等的所有项目。
但是,由于这些表之间没有真正的关联,我无法将用户分配给项目(除非我在db中手动添加它们)。
Project_helper 表引用了用户&项目,但用户和项目都没有引用project_helper或彼此。
据我了解,我通过创建project_helper表创建了多对多的关系,如果我从这个tut中正确地学到了: http://www.phpknowhow.com/mysql/many-to-many-relationships/ 我做得对,其余的工作都是由查询管理的。
我想要实现的是,在创建新项目时,必须至少分配一个用户(稍后,当有一个登录系统时,它将成为项目的创建者作为其第一个成员,但现在它没有&# 39;我想是的。)
应该有一个下拉输入来选择用户并将其分配给该项目。 第三个表(projects_helper)没有控制器或视图或其他内容。我希望没有必要制作一个。
我是否对db做错了,可以自动生成(烘焙)吗?
// model/table/ProjectsTables
class ProjectsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('projects');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('ProjectsGroup', [
'foreignKey' => 'project_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('title', 'create')
->notEmpty('title');
$validator
->requirePresence('description', 'create')
->notEmpty('description');
$validator
->requirePresence('logo', 'create')
->notEmpty('logo');
return $validator;
}
模型/表/用户表
class UsersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->hasMany('ProjectsGroup', [
'foreignKey' => 'user_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('login', 'create')
->notEmpty('login');
$validator
->requirePresence('password', 'create')
->notEmpty('password');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email');
$validator
->requirePresence('avatar', 'create')
->notEmpty('avatar');
$validator
->dateTime('lastvisited')
->requirePresence('lastvisited', 'create')
->notEmpty('lastvisited');
$validator
->integer('lastip')
->requirePresence('lastip', 'create')
->notEmpty('lastip');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['login']));
$rules->add($rules->isUnique(['email']));
return $rules;
}
模板/项目/ add.ctp
<?php
/**
* @var \App\View\AppView $this
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Projects'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Projects Group'), ['controller' => 'ProjectsGroup', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Projects Group'), ['controller' => 'ProjectsGroup', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="projects form large-9 medium-8 columns content">
<?= $this->Form->create($project) ?>
<fieldset>
<legend><?= __('Add Project') ?></legend>
<?php
echo $this->Form->control('title');
echo $this->Form->control('description');
echo $this->Form->control('logo');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
控制器/ ProjectsController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* Projects Controller
*
* @property \App\Model\Table\ProjectsTable $Projects
*/
class ProjectsController extends AppController
{
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$projects = $this->paginate($this->Projects);
$this->set(compact('projects'));
$this->set('_serialize', ['projects']);
}
/**
* View method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$project = $this->Projects->get($id, [
'contain' => ['ProjectsGroup']
]);
$this->set('project', $project);
$this->set('_serialize', ['project']);
}
/**
* Add method
*
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$project = $this->Projects->newEntity();
if ($this->request->is('post')) {
$project = $this->Projects->patchEntity($project, $this->request->getData());
if ($this->Projects->save($project)) {
$this->Flash->success(__('The project has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The project could not be saved. Please, try again.'));
}
$this->set(compact('project'));
$this->set('_serialize', ['project']);
}
/**
* Edit method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$project = $this->Projects->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$project = $this->Projects->patchEntity($project, $this->request->getData());
if ($this->Projects->save($project)) {
$this->Flash->success(__('The project has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The project could not be saved. Please, try again.'));
}
$this->set(compact('project'));
$this->set('_serialize', ['project']);
}
/**
* Delete method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$project = $this->Projects->get($id);
if ($this->Projects->delete($project)) {
$this->Flash->success(__('The project has been deleted.'));
} else {
$this->Flash->error(__('The project could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
干杯。
编辑。 正如我在想的那样,我可以添加一个&#39;作者&#39;列到项目表,因此创建项目的用户将自动成为其作者。但是,它仍然没有解决将其他用户分配给项目的问题。我希望通过项目控制器和工作完成它。项目添加/编辑视图。