/**
* Return a SqlBuilder object which has set the table and optionally
* assigned values to columns based on this instances' properties. This is used in
* insert(), update(), and delete()
*
* @param ModelDescriptor $descriptor
* @param boolean $useAssignment
* @param boolean $excludePrimaryKey
* @return SqlBuilder
*/
protected function assignmentSqlForThisObject(ModelDescriptor $descriptor, $useAssignment = true, $excludePrimaryKey = false)
{
$sqlBuilder = $descriptor->getSource()->getBuilder();
$sqlBuilder->from($descriptor->getTable());
if (empty($descriptor->columns)) {
throw new RecessException('The "' . $descriptor->getTable() . '" table does not appear to exist in your database.', get_defined_vars());
}
// We need the details to pass the column data type to assign()
$columnDetails = $descriptor->getSource()->getTableDescriptor($descriptor->getTable())->getColumns();
foreach ($this as $column => $value) {
if ($excludePrimaryKey && $descriptor->primaryKey == $column) {
continue;
}
if (in_array($column, $descriptor->columns) && isset($value)) {
if ($useAssignment) {
$sqlBuilder->assign($column, $value, $columnDetails[$column]->type);
} else {
$sqlBuilder->equal($column, $value);
}
}
}
return $sqlBuilder;
}