Error coding "Associations" Guide

76 views
Skip to first unread message

dhga...@gmail.com

unread,
Feb 10, 2021, 11:21:33 PM2/10/21
to CFWheels
QUESTION: Is there a video on implementing "cfwheels associations and Nested Properties"?

I tried implementing "Associations" https://guides.cfwheels.org/docs/associations  and "nested properties" in my app, but could not get it to work.  I was able to get the belongsto() to work, but every time I use hasMany() I kept getting " Wheels.AssociationNotFound An association named zselecttaxcat could not be found on the inventoryitem model. " errors.  But that is not the problem I am asking about here.  

QUESTION: Does anyone have an simple example app of using; one to many associations and nested properties?

So to learn more about these concepts, I decided to switch gears and carefully follow the "Associations" guide, STEP by STEP, from top to bottom. I create the various tables in my database (mySql) , and put dummy data in them.  I have coded the first  nine sections of code on the "Associations" page.  . So now I am testing the "Using the include Argument in findAll()" section:


As you can see , below, from the SQL dump, post_id does exist in the  `authors` table.

When I try this code, I get error:
 Element post_id is undefined in a CFML structure referenced as part of an expression.

The error occurred in D:/program_files/CommandBox/aod_inv/wheels/model/sql.cfm: line 825
Called from D:/program_files/CommandBox/aod_inv/wheels/model/sql.cfm: line 252
Called from D:/program_files/CommandBox/aod_inv/wheels/model/sql.cfm: line 230
Called from D:/program_files/CommandBox/aod_inv/wheels/model/read.cfm: line 273
Called from D:/program_files/CommandBox/aod_inv/controllers/posts.cfc: line 15
Called from D:/program_files/CommandBox/aod_inv/wheels/global/cfml.cfm: line 160
Called from D:/program_files/CommandBox/aod_inv/wheels/controller/processing.cfm: line 125
Called from D:/program_files/CommandBox/aod_inv/wheels/controller/processing.cfm: line 92
Called from D:/program_files/CommandBox/aod_inv/wheels/dispatch/functions.cfm: line 199
Called from D:/program_files/CommandBox/aod_inv/wheels/index.cfm: line 6
Called from D:/program_files/CommandBox/aod_inv/rewrite.cfm: line 2
Called from D:/program_files/CommandBox/aod_inv/wheels/events/onrequest.cfm: line 4

823 : local.toAppend = ListAppend( 824 : local.toAppend, 825 : "#local.class.$classData().tableName#.#local.class.$classData().properties[local.first].column# = #local.tableName#.#local.associatedClass.$classData().properties[local.second].column#" 826 : ); 827 : if (!arguments.includeSoftDeletes && local.associatedClass.$softDeletion()) {


I have attached a .zip of ALL the code. but here is the code for the two models and two controllers.

mySQL:

CREATE TABLE `authors` (
  `id` int NOT NULL,
  `post_id` int DEFAULT NULL COMMENT 'Foreign key to the posts table',
  `name` varchar(45) DEFAULT 'name of author 01',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `authors` VALUES
   (51,1,'Danielle Steel')
  ,(52,2,'William Shakespeare')
  ,(53,3,'Dr. Seuss')
  ,(54,4,'Stephen King');

CREATE TABLE `posts` (
  `id` int NOT NULL,
  `name` varchar(45) DEFAULT 'name of post 01',
  `createdAt` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `posts` VALUES
   (1,'name of post 01','2001-01-01')
  ,(2,'name of post 02','2002-02-02')
  ,(3,'name of post 03','2003-03-03')
  ,(4,'name of post 04','2004-04-04');


// controllers/authors.cfc
component extends="Controller" {
    function config() {
        verifies(except="index,new,create", params="key", paramsTypes="integer", handler="objectNotFound");
    }
    function index() {
      // authors= model("author").findAll();  
             //
see 2nd code section on; https://guides.cfwheels.org/docs/associations#using-the-include-argument-in-findall

        authors = model("author").findAll(include="posts");
    }

// models/ author.cfc
component extends="Model" {
    function config() {
    }
}

//    controllers/ posts.cfc
component extends="Controller" {   
    function config() {
        verifies(except="index,new,create", params="key", paramsTypes="integer", handler="objectNotFound");
    }
    function index() {
        //posts=model("post").findAll();
               
        // see https://guides.cfwheels.org/docs/associations#using-the-include-argument-in-findall
        posts = model("post").findAll(include="author");
    }
   // ...
}

// models/ post.cfc
component extends="Model" {       
    function config() {
        // A post    has many    comments
        hasMany("comments");    // plural
       
        // see https://guides.cfwheels.org/docs/associations#breaking-the-convention
        // An author belongs to a post
        belongsTo(name="author", foreignKey="post_id");    // singular
    }
}

QUESTION: Any idea as to what I am missing / doing wrong ? 

SUGGESTION: The associations and nested properties guides should be split up and rewritten into four (or more) separate "tutorials"; {Just like the " Beginner Tutorial: Hello Database" guide} Something like;
1) Intro to associations and nested Properties
2) Tutorial: belongsTo()
3) Tutorial: HasOne()
4) Tutorial: HasMany()


TestAssociations.zip

Tom King

unread,
Mar 10, 2022, 2:31:33 PM3/10/22
to CFWheels
Sorry your post seems to have got lost a bit.

There's an example (albeit limited) of nestedproperties in


A permission hasMany (nested) userpermissions (and role permissions).

Reply all
Reply to author
Forward
0 new messages