I have problem with dynamiccaly adding form fields in CakePHP app and I don't know how to solve it. I want to have form in EventsController/add.ctp for add Event where I want to have fields Events.name, Dates.from, Dates.to, Dates.endregister, Dates.location_id, {optional more Dates.from, Dates.to, ...}, Terms_mem.teacher_id {and optional more Terms_mem.teacher_id} My tables are:
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(150) NOT NULL
);
CREATE TABLE `dates` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`from` datetime NOT NULL,
`to` datetime NOT NULL,
`endregister` datetime,
`event_id` int(11) NOT NULL,
`location_id` int(11) NOT NULL,
FOREIGN KEY (`event_id`) REFERENCES `events`(`id`),
FOREIGN KEY (`location_id`) REFERENCES `locations`(`id`)
);
CREATE TABLE `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`street` varchar(70),
`city` varchar(70) NOT NULL
);
CREATE TABLE `dates_mem` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`teacher_id` int(11) NOT NULL,
`date_id` int(11) NOT NULL,
FOREIGN KEY (`teacher_id`) REFERENCES `users`(`id`),
FOREIGN KEY (`date_id`) REFERENCES `dates`(`id`)
)
So form would look like:
<?php echo $this->Form->create('Event'); ?>
<fieldset>
<?php
// events
echo $this->Form->input('name');
// dates
echo $this->Form->input('from');
echo $this->Form->input('to');
echo $this->Form->input('endregister');
echo $this->Form->input('location_id');
/* HERE optional dynamically add next inputs for dates (from, to, ...) */
// teachers
echo $this->Form->input('teacher_id');
/* HERE optional dynamically add next inputs for teachers(teacher_id) */
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
And after all that fields save to corresponding tables. Is this possible in CakePHP, version 2.4? If yes, could you help me with it?
EDIT:
burzum wrote:
$this->Form->input('Date.0.from');
$this->Form->input('Date.0.to');
$this->Form->input('Date.1.from');
$this->Form->input('Date.1.to');
Is it possible to do it as: ? Thus fields Date.1.from and Date.1.to add to form dynamically after click on button add next date
$this->Form->input('Date.0.from');
$this->Form->input('Date.0.to');
// button add next date
$this->Form->input('Date.1.from'); // after click on add next date
$this->Form->input('Date.1.to'); // after click on add next date
// button add next date
$this->Form->input('Date.2.from'); // after click on add next date
$this->Form->input('Date.2.to'); // after click on add next date
// button add next date
Have you tried to read the manual? If not read the manual it is explained there in detail in the section "Saving your data". See this part.
In short, first the view
$this->Form->input('FirstModel.field1');
$this->Form->input('SecondModel.field1');
$this->Form->input('SecondModel.field2');
$this->Form->input('Date.0.from');
$this->Form->input('Date.0.to');
$this->Form->input('Date.1.from');
$this->Form->input('Date.1.to');
// ...
Controller:
$this->saveAll($this->request->data);
Yes, you can add fields dynamically on the fly, use Javascript to inject additional fields into the DOM. Either via a JS template or via AJAX. Just make sure the form inputs you generate follow the CakePHP conventions and that you white list the JS generated fields. Also make sure you validate the inputs you've white listed very strict to avoid getting things added you don't want. You should use the security component if you don't use it already.
Related
I have created a table and now find that when I try and delete a row or change a field, I get the following message:
Unexpected update count received (Actual: 0, Expected: 1). All changes will be rolled back.
I saw another question about this that said to create a unique field and try again. I dropped the table, recreated it with a unique field and am still getting the same error.
The code to create the table is:
create table 'transcribercertifications' (
id int(11) not null unique auto_increment,
tid int(11) not null,
certId varchar(32) not null,
regNum int(11) not null,
expiration date
);
I have been deleting and updating 'by hand' in PhpStorm. Any help fixing this would be much appreciated.
You need to make your id column as a primary key. Try this one:
create table 'transcribercertifications' (
id int(11) not null primary key auto_increment,
tid int(11) not null,
certId varchar(32) not null,
regNum int(11) not null,
expiration date
);
I have a simple ACL system in PHP and MYSQL started. I need help finishing it though...
I have 2 Database tables shown below...
user_link_permissions : Holds a record for every user, on every entity/link that permissions apply to...
--
-- Table structure for table `user_link_permissions`
--
CREATE TABLE IF NOT EXISTS `user_link_permissions` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`user_id` int(30) NOT NULL,
`link_id` int(30) NOT NULL,
`permission` int(2) NOT NULL DEFAULT '0',
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2055 ;
intranet_links : Is basically the entity that the permission gives or revokes user access to
--
-- Table structure for table `intranet_links`
--
CREATE TABLE IF NOT EXISTS `intranet_links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`description` text NOT NULL,
`url` varchar(255) DEFAULT NULL,
`notes` text,
`user_login` varchar(255) DEFAULT NULL,
`user_pw` varchar(255) DEFAULT NULL,
`active` int(2) NOT NULL DEFAULT '1',
`sort_order` int(11) DEFAULT NULL,
`parent` int(10) NOT NULL DEFAULT '1',
`local_route` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `local_route` (`local_route`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;
To save these permissions settings I have a matrix style grid like this below where each checkbox is a record in the user_link_permissions table...
I need help creating a simple ACL function in PHP which can check if a user has permission or not to view a link/entity based on the database results.
On page load I am thinking I can query the user_link_permissions DB table for all records with a matching user ID of the logged in user and store them to a session array variable.
A function could then use that array to check for a link/entity permission using that array value on the entity key.
I just can't visualize how it might look at the moment in PHP.
Any help please?
function aclCanAccess($user_id, $entity_id){
}
$entity_id = 123;
if(aclCanAccess(1, $entity_id){
// yes user can see this item
}else{
// NO user permission denied
}
I will leave writing the code to you for fun.
Assume you are storing all the previously queried permissions in a variable called $_SESSION['acl']
Your ACL function should:
check the session if you already queried that entity
if it is not set, read it from the db
in short
function..... {
if(!isset($_SESSION['acl'][$entity_id])) {
$_SESSION['acl'][$entity_id] = query here to return to you if he has access or not
}
return $_SESSION['acl'][$entity_id];
}
You can also read the entire array when you log in the user. That might also be appropriate. In that case you should be able to just
return $_SESSION['acl'][$entity_id];
But I would then try and catch an exception in case it is not set.
I try to insert Data in the temporary Table, but when i call select last_insert_id() it return allways the same number. I have set the autoincement setting.
In the normal Table works, but when i Copy the normal Table to a temporary Table, then i have the problem with the select last_insert_id().
This is my code:
CREATE TEMPORARY TABLE IF NOT EXISTS suchergebnisse_temp (SELECT * from suchergebnisse);
INSERT INTO suchergebnisse_temp SET datensatzid='2865', datum='2015-05-13 00:00:00', tabelle='task', sortierung1='1';
SELECT LAST_INSERT_ID();
The Normal Table:
CREATE TABLE IF NOT EXISTS `suchergebnisse` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tabelle` varchar(100) NOT NULL DEFAULT '0',
`datensatzid` bigint(20) NOT NULL DEFAULT '0',
`datum` datetime NOT NULL,
`sortierung1` int(11) NOT NULL COMMENT 'Priorität',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Temporäre Tabelle.';
Can you help me? Thanks :)
I forgot to add the key explicit to the temporary Table. It needs to execute the following code:
ALTER TABLE `suchergebnisse_temp`
CHANGE COLUMN `id` `id` BIGINT(20) NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`);
Because with:
CREATE TEMPORARY TABLE IF NOT EXISTS suchergebnisse_temp (SELECT * from suchergebnisse);
copys only the Field Data but not the Key Data. The Key Data must be declared separately.
Hey how would I be able to duplicate my only auto increment key to another key, basically I want my (' id ') to display the same information on my (' user_id '), here is the code:
CREATE TABLE IF NOT EXISTS `".$db_table_prefix."users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`user_name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`id`)
How would I be able to input the same information from my id to my user_id?
Not sure what you mean but if you want to have the same value repeted two times in the same record It's pointless and redundant.
You can use the SQL aliases to achive what you want:
SELECT id as user_id FROM ...
If you really need to sync up the two field of your table you can do:
UPDATE table SET user_id = id WHERE user_id != id
Not sure why you would want to do this, but if you want to duplicate the information after an INSERT you would need to fetch the new ID and then perform an UPDATE
// get the newly inserted ID
$new_id = $db->insert_id;
// perform the update on the table
$db->query("UPDATE users SET user_id=".$db->escape($new_id)." WHERE id=".$db->escape($new_id));
Also, in your table definition the fields don't match: int(11) vs. int(10).
I'm working on a web site where users can post articles with this table structure :
CREATE TABLE IF NOT EXISTS `articles` (
`id_articles` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_users` int(10) unsigned NOT NULL,
`articles` text NOT NULL,
PRIMARY KEY (`id_articles`),
UNIQUE KEY `id_articles` (`id_articles`),
KEY `id_users` (`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Each user can 'like' the articles.
Is that the right way below to create a 'like table' :
CREATE TABLE IF NOT EXISTS `articles_likes` (
`id_articles` int(10) unsigned NOT NULL,
`id_users` int(10) unsigned NOT NULL,
KEY `id_articles` (`id_articles`,`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
It is correct but you will want to add separte indexes on id_articles and id_users (also you might want to name the columns 'id_article' and 'id_user' for sanity).
CREATE TABLE IF NOT EXISTS `article_likes` (
`id_article` int(10) unsigned NOT NULL,
`id_user` int(10) unsigned NOT NULL,
KEY `id_article` (`id_article`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB;
The reason you want separate indexes is because in mysql if you create an index on columns (A, B) that index will be used in queries having in the where clause column A, or columns A and B.
In your case for example if you made a query "SELECT * FROM article_likes WHERE id_user=X" this query would not use an index.
An ever better option would be to add a combined index and a separate index on the second column from the combined index. Like this:
CREATE TABLE IF NOT EXISTS `article_likes` (
`id_article` int(10) unsigned NOT NULL,
`id_user` int(10) unsigned NOT NULL,
KEY `id_article_user` (`id_article`, `id_user`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB;
This way you would have optimal performance on queries like 'WHERE id_user=X', "WHERE id_article=X', "WHERE id_article=X AND id_user=Y"
This is a valid way Chris. You can use COUNT() to match the id_articles in the articles_likes table against the current article you are viewing in articles.
$articles_id = 23;
mysql_query("SELECT COUNT(*) FROM articles_likes
WHERE id_articles = ".$articles_id);
You can also just leave COUNT() (MySQL) out and instantly know which users are the "likers" of the articles and use count() (PHP) on the returned Array to duplicate the effect of COUNT() in MySQL.
i would have a total of 3 tables. an articles table, and the user id could be a column in that for users who submit articles , but you need a separate user table since not all users will submit articles (i am assuming), and then a 3rd table for likes, that takes the primary key from users and the primary key from articles and uses them as foreign keys. so each time an article is liked, an entry is made in the 3rd table