Hi everyone,
I thought I got this association thing down. Apparently not… This may
not be an association issue but than again I’ve been wrong before. So
here’s setup…
[Project.cfc - model]
<cffunction name="init">
<cfset belongsTo(name="customer", foreignKey="customerid")>
<cfset belongsTo(name="user", joinType="outer")>
<cfset hasMany(name="tasks", dependent="deleteAll")>
</cffunction>
[task.cfc model]
<cffunction name="init">
<cfset belongsTo(name="project", foreignKey="projectid")>
</cffunction>
[projects.cfc – controller]
<cffunction name="show">
<cfset project = model("Project").findByKey(key="#params.key#",
include="customer,user,tasks", returnAs="query", order="createdAt
DESC")>
</cffunction>
CFdump on projects/show/5 produces this SQL:
SQL:
SELECT
projects.id,projects.pname,projects.dueDate,projects.descriptions,projects.customerid,projects.userid,projects.createdAt,projects.updatedAt,projects.deletedAt,customers.cfname,customers.clname,customers.cemail,customers.cphone,customers.cfax,customers.ccompanyname,customers.caddress,customers.ccity,customers.cstate,customers.czip,customers.authcode,customers.createdAt
AS customercreatedAt,customers.updatedAt AS
customerupdatedAt,customers.deletedAt AS
customerdeletedAt,users.ufname,users.ulname,users.uemail,users.uphone,users.authcode
AS userauthcode,users.role,users.isactive,users.createdAt AS
usercreatedAt,users.updatedAt AS userupdatedAt,users.deletedAt AS
userdeletedAt,
tasks.id AS taskid,tasks.projectid,tasks.userid AS
taskuserid,tasks.tname,tasks.tdescriptions,tasks.tlog,tasks.tstatus,tasks.createdAt
AS taskcreatedAt,tasks.updatedAt AS taskupdatedAt,tasks.DeletedAt AS
taskDeletedAt
FROM projects INNER JOIN customers ON projects.customerid =
customers.id LEFT OUTER JOIN users ON projects.userid =
users.id LEFT
OUTER JOIN tasks ON
projects.id = tasks.projectid
WHERE
(
(
projects.id =
5
) AND
projects.id IN
(5)
) AND (
projects.deletedAt IS NULL AND customers.deletedAt IS NULL AND
users.deletedAt IS NULL AND tasks.DeletedAt IS NULL
)
ORDER BY projects.createdAt DESC,
projects.id ASC
Everything works fine until I delete a task from a project and wheels
populate the DeletedAt column with a date. The same query return
nothing because of this “AND tasks.DeletedAt IS NULL “.
Question one: How do I get around this, I want to return the projects
even thou the project have deleted tasks. Is there a way to disable or
remove “AND tasks.DeletedAt IS NULL” from the query?
Question Two: why is there a “AND
projects.id IN (5)” in addition to
“
projects.id = 5”? Am I missing something?
Thank you in advance for you response!