There is no documentation on cakephp.org and I am unable to find one on google. Please link me some documentation or supply one!
The translate behavior is another of CakePHP's very useful but poorly documented features. I've implemented it a couple of times with reasonable success in multi-lingual websites along the following lines.
Firstly, the translate behavior will only internationalize the database content of your site. If you've any more static content, you'll want to look at Cake's __('string') wrapper function and gettext (there's some useful information about this here)
Assuming there's Contents that we want to translate with the following db table:
CREATE TABLE `contents` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) default NULL,
`body` text,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The content.php model then has:
var $actsAs = array('Translate' => array('title' => 'titleTranslation',
'body' => 'bodyTranslation'
));
in its definition. You then need to add the i18n table to the database thusly:
CREATE TABLE `i18n` (
`id` int(10) NOT NULL auto_increment,
`locale` varchar(6) NOT NULL,
`model` varchar(255) NOT NULL,
`foreign_key` int(10) NOT NULL,
`field` varchar(255) NOT NULL,
`content` mediumtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then when you're saving the data to the database in your controller, set the locale to the language you want (this example would be for Polish):
$this->Content->locale = 'pol';
$result = $this->Content->save($this->data);
This will create entries in the i18n table for the title and body fields for the pol locale. Finds will find based on the current locale set in the user's browser, returning an array like:
[Content]
[id]
[titleTranslation]
[bodyTranslation]
We use the excellent p28n component to implement a language switching solution that works pretty well with the gettext and translate behaviours.
It's not a perfect system - as it creates HABTM relationships on the fly, it can cause some issues with other relationships you may have created manually, but if you're careful, it can work well.
For anyone searching the same thing, cakephp updated their documentation. For Translate Behavior go here..
Related
I'm trying to implement total unique page view count to my webpage, Any one knows that how to do that just give me an idea to implement using CakePHP 3.0
create table for page view
ex:
CREATE TABLE `pageview` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page` text NOT NULL,
`userip` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
then store visitor ip address into your table using
// gets the user IP Address
$userip=$_SERVER['REMOTE_ADDR'];
You can do it by checking both COOKIE and IP
There is a Plugins written by CakeManager which give those facility with proper functionality
https://github.com/cakemanager/cakephp-analyzer
I'm facing issue in Query that was written through normal CakePHP version 2.3.4
The problem occurs rarely [not frequently] - only few row gets auto deleted.
There is neither any cron nor any action in Controller-file to execute "Delete" command/query.
But why this auto-deletion is happening is miserable for me till now.
The Table structure is as provided below:
CREATE TABLE IF NOT EXISTS `bulk_orders` (
`id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`),
UNIQUE KEY `order_no` (`order_no`)
) EN
GINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16270 ;
CREATE TABLE IF NOT EXISTS `bulk_order_lines` (
`id` int(10) NOT NULL auto_increment,
`order_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=34711 ;
Auto delete query is executed like below :
Delete from bulk_orders where ID=1234;
Can anybody please help me to find the solution on this?
Your question is very vague.
I assume you have baked your CRUD controller actions and templates via bake?
Usually this is the developers fault then - failure by design.
The HTTP specs are pretty clear about what protocol type to use for modifying the DB: POST
Additionally, it should also be secured via auth or some additional measure to avoid abusing.
But using POST here instead of GET prevents BOTs and people with little knowledge of deleting your content - by accident or purpose.
$this->Form->postLink()
is what you should be using.
And the action itself should contain
$this->request->allowMethod('post');
etc.
Please confirm that this is what you got in your code already.
I have a database with the following tables:
CREATE TABLE `visitors` (
`name` varchar(64) not null,
`id` int(10) unsigned not null auto_increment,
# ...more fields here
PRIMARY KEY (`id`),
UNIQUE KEY (`name`)
);
CREATE TABLE `credentials` (
`id` int(10) unsigned not null auto_increment,
`visitor_id` int(10) unsigned,
`type` enum('password','openid','google','facebook') not null,
`token` char(40) not null,
`modified` datetime,
`hint` varchar(64),
PRIMARY KEY (`id`),
KEY `visitor` (`visitor_id`),
KEY `token` (`token`)
);
After thinking about this for a awhile, I've decided that this is "right" for e.g. normalization and allowing visitors to have multiple login credentials, including multiple passwords.
However, I'd like to use Cake's ACL features, and AuthComponent assumes that hashed passwords are stored in the same table as user (visitor) information. What's the best way to work around this? Do I have to use Auth->login(), or is there a better way?
If you're using CakePHP 2.x this can be archived pretty simple by creating a new authentication handler. Looking at the existing Form handler will also help you. You should be able to extend it or copy the code into a new handler and modify it to match your needs.
Read the authentication chapter in the book. It has a section just about creating a custom handler but to get an understanding of how the whole thing works I really suggest you to read the whole page.
i am trying to use cakes baking console and when i use cake bake model all
i am getting the following error
Attendance Table Structure
CREATE TABLE IF NOT EXISTS `mydb`.`attendences` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`dateTime` DATETIME NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
Rather than baking it all at once, do it table-by-table and model-controller-view separately. That way you'll be able to pin down the error.
Have you created any fixtures correctly? See http://book.cakephp.org/view/360/Creating-fixtures and check the syntax is correct, particularly in the array definitions.
I have only two tables in my database with a one-to-many relationship between them (user hasMany messages) and am trying to get basic CRUD functionality going. Bake detects the associations correctly and specifies them correctly inside the model classes, but in controllers and views it looks like Cake doesn't know anything about those associations -- I don't even get a select tag for user_id when I go add a new message. Has anyone come across this problem before? What can I be doing wrong?
Table structure appears to be fine:
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
email varchar(255) NOT NULL,
created datetime NOT NULL,
modified datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`content` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
If you're using the console to bake your MVC files you could try that.
First bake the model files. Then bake the controller files using scaffold.
Then bake the view files.
Finally go back and bake the controller files without scaffold.
This should get you all basic CRUD functionality with all the associations you may have.
Hope that helps ...