从 Propel 的结果集中排除连接关系对象的方法?
Way to exclude a joined relation object from the result set in Propel?
我有 4 个表,代表一个简单的调查数据库设置:
table name: (delimited columns)
---------------------------------------------------------------
survey: (id, title)
surveyQuestion: (id, title)
surveyAnswer: (id, surveyQuestionID, title, sortOrder)
surveyToSurveyQuestion: (surveyID, surveyQuestionID, sortOrder)
这是我用来提取所有调查以及所有相关问题和答案的查询:
$query = SurveyQuery::create()
->joinWith('SurveyToSurveyQuestion')
->useSurveyToSurveyQuestionQuery()
->orderBySurveyId()
->orderBySortOrder()
->joinWith('SurveyQuestion')
->useSurveyQuestionQuery()
->joinWith('SurveyAnswer')
->useSurveyAnswerQuery()
->orderBySortOrder()
->endUse()
->endUse()
->endUse();
但是,随着我添加更多调查,多对多“SurveyToSurveyQuestion
”关系变得臃肿,特别是如果调查共享问题。
有没有办法从结果数据集中排除“SurveyToSurveyQuestion
”数据?
编辑:
正在附加数据库架构。
<table name="survey" idMethod="native" phpName="Survey">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="statusID" phpName="StatusID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<column name="createdDate" phpName="CreatedDate" type="TIMESTAMP" default="0000-00-00 00:00:00"/>
<index name="INDEX_StatusID">
<index-column name="statusID"/>
</index>
<foreign-key foreignTable="status">
<reference local="statusID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyQuestion" idMethod="native" phpName="SurveyQuestion">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyQuestionTypeID" phpName="SurveyQuestionTypeID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<index name="INDEX_SurveyQuestionTypeID">
<index-column name="surveyQuestionTypeID"/>
</index>
<foreign-key foreignTable="surveyQuestionType">
<reference local="surveyQuestionTypeID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyQuestionType" idMethod="native" phpName="SurveyQuestionType">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="title" phpName="Title" type="VARCHAR" required="true"/>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyAnswer" idMethod="native" phpName="SurveyAnswer">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyQuestionID" phpName="SurveyQuestionID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<column name="sortOrder" phpName="SortOrder" type="INTEGER"/>
<index name="INDEX_SurveyQuestionID">
<index-column name="surveyQuestionID"/>
</index>
<foreign-key foreignTable="surveyQuestion">
<reference local="surveyQuestionID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyToSurveyQuestion" idMethod="native" phpName="SurveyToSurveyQuestion">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyID" phpName="SurveyID" type="INTEGER" required="true"/>
<column name="surveyQuestionID" phpName="SurveyQuestionID" type="INTEGER" required="true"/>
<column name="sortOrder" phpName="SortOrder" type="INTEGER"/>
<index name="INDEX_SurveyID">
<index-column name="surveyID"/>
</index>
<index name="INDEX_SurveyQuestionID">
<index-column name="surveyQuestionID"/>
</index>
<foreign-key foreignTable="survey">
<reference local="surveyID" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="surveyQuestion">
<reference local="surveyQuestionID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
编辑: 使用 join
代替 joinWith
为我完成了这项工作。谢谢@kripple。
您不需要 ->joinwith()
即可使用 ->useSurveyToSurveyQuestion()
您可以排除导致连接对象不存在的连接调用。
此外,我会在每项调查中使用方法来获得各自的问题和答案
$surveys = SurveyQuery::create()->filterByXXX()->find()
foreach($survey as $s){
//Grab each survey's respective data
$QandA = $s->getQuestionsAndAnswers();
// do something with the data.
$otherClass->doStuffWithData($QandA);
}
这样做无疑会增加访问数据库的次数,但我认为整体效率会好很多。
我有 4 个表,代表一个简单的调查数据库设置:
table name: (delimited columns)
---------------------------------------------------------------
survey: (id, title)
surveyQuestion: (id, title)
surveyAnswer: (id, surveyQuestionID, title, sortOrder)
surveyToSurveyQuestion: (surveyID, surveyQuestionID, sortOrder)
这是我用来提取所有调查以及所有相关问题和答案的查询:
$query = SurveyQuery::create()
->joinWith('SurveyToSurveyQuestion')
->useSurveyToSurveyQuestionQuery()
->orderBySurveyId()
->orderBySortOrder()
->joinWith('SurveyQuestion')
->useSurveyQuestionQuery()
->joinWith('SurveyAnswer')
->useSurveyAnswerQuery()
->orderBySortOrder()
->endUse()
->endUse()
->endUse();
但是,随着我添加更多调查,多对多“SurveyToSurveyQuestion
”关系变得臃肿,特别是如果调查共享问题。
有没有办法从结果数据集中排除“SurveyToSurveyQuestion
”数据?
编辑:
正在附加数据库架构。
<table name="survey" idMethod="native" phpName="Survey">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="statusID" phpName="StatusID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<column name="createdDate" phpName="CreatedDate" type="TIMESTAMP" default="0000-00-00 00:00:00"/>
<index name="INDEX_StatusID">
<index-column name="statusID"/>
</index>
<foreign-key foreignTable="status">
<reference local="statusID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyQuestion" idMethod="native" phpName="SurveyQuestion">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyQuestionTypeID" phpName="SurveyQuestionTypeID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<index name="INDEX_SurveyQuestionTypeID">
<index-column name="surveyQuestionTypeID"/>
</index>
<foreign-key foreignTable="surveyQuestionType">
<reference local="surveyQuestionTypeID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyQuestionType" idMethod="native" phpName="SurveyQuestionType">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="title" phpName="Title" type="VARCHAR" required="true"/>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyAnswer" idMethod="native" phpName="SurveyAnswer">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyQuestionID" phpName="SurveyQuestionID" type="INTEGER" required="true"/>
<column name="code" phpName="Code" type="VARCHAR" required="true"/>
<column name="sortOrder" phpName="SortOrder" type="INTEGER"/>
<index name="INDEX_SurveyQuestionID">
<index-column name="surveyQuestionID"/>
</index>
<foreign-key foreignTable="surveyQuestion">
<reference local="surveyQuestionID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
<table name="surveyToSurveyQuestion" idMethod="native" phpName="SurveyToSurveyQuestion">
<column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
<column name="surveyID" phpName="SurveyID" type="INTEGER" required="true"/>
<column name="surveyQuestionID" phpName="SurveyQuestionID" type="INTEGER" required="true"/>
<column name="sortOrder" phpName="SortOrder" type="INTEGER"/>
<index name="INDEX_SurveyID">
<index-column name="surveyID"/>
</index>
<index name="INDEX_SurveyQuestionID">
<index-column name="surveyQuestionID"/>
</index>
<foreign-key foreignTable="survey">
<reference local="surveyID" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="surveyQuestion">
<reference local="surveyQuestionID" foreign="id"/>
</foreign-key>
<vendor type="mysql">
<parameter name="Engine" value="InnoDB"/>
</vendor>
</table>
编辑: 使用 join
代替 joinWith
为我完成了这项工作。谢谢@kripple。
您不需要 ->joinwith()
即可使用 ->useSurveyToSurveyQuestion()
您可以排除导致连接对象不存在的连接调用。
此外,我会在每项调查中使用方法来获得各自的问题和答案
$surveys = SurveyQuery::create()->filterByXXX()->find()
foreach($survey as $s){
//Grab each survey's respective data
$QandA = $s->getQuestionsAndAnswers();
// do something with the data.
$otherClass->doStuffWithData($QandA);
}
这样做无疑会增加访问数据库的次数,但我认为整体效率会好很多。