I couldn't get it to work with the solution proposed by ndm alone. Apparently in that case, cake bake gets the idea it is a self-referencing field. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to define a unique alias for a self joining association? Ask Question. Asked 5 years, 11 months ago. Active 5 years, 11 months ago.
Viewed times. Improve this question. Up to 3. I also tried with postman, nothing! What's wrong? On my cakephp based site, when i do multiple requests simultaneously, cakephp logs me out. What happens exactly is, I click on one of the link on my site What can be the solution for this? I want to print formatted dates on my page. Everything works fine before December Is this still available in Cake 3? Or how can I use something else please explain. That doesn It is not mandatory to follow CakePHP conventions, you can override the name of any foreignKey in your associations definitions.
Nevertheless, sticking to conventions will make your code less repetitive, easier to read and to maintain. Once you create the UsersTable and AddressesTable classes, you can make the association with the following code:. If you need more control, you can define your associations using the setters.
For example, you might want to limit the association to include only certain records:. If a column is shared by multiple hasOne associations, you must qualify it with the association alias. This is the same name used when getting an instance of the table. The default value is the name of the association.
In this case we set it to true so that deleting a User will also delete her associated Address. When false , deleteAll is used to remove associated data and no callbacks are triggered. Once this association has been defined, find operations on the Users table can contain the Address record if it exists:. The belongsTo association is a natural complement to the hasOne and hasMany associations - it allows us to see related data from the other direction.
Once this association has been defined, find operations on the Addresses table can contain the User record if it exists:. Relying on the example above, we have passed an array containing the desired composite keys to setForeignKey. You can setup it manually with setBindingKey :.
Like hasOne associations, foreignKey is in the other Comments table and bindingKey is in the current Articles table. In this example, Comment records will be deleted when their associated Article record has been deleted. If the foreign key is a nullable column or if dependent is true records will be orphaned. Once this association has been defined, find operations on the Articles table can contain the Comment records if they exist:.
You may want to cache the counts for your hasMany associations. For example, the comment count on any given article is often cached to make generating lists of articles more efficient. You can use the CounterCacheBehavior to cache counts of associated records. You should make sure that your database tables do not contain columns that match association property names.
If for example you have counter fields that conflict with association properties, you must either rename the association property, or the column name. The main difference between hasMany and BelongsToMany is that the link between the models in a BelongsToMany association is not exclusive.
For example, we are joining our Articles table with a Tags table. I can also use it on the next article I write.
0コメント