00001 <?php
00002
00014 class SDRProjectField extends ModelBase {
00016 public static $ATTRIBUTES = array(
00017 'projectId' => array(
00018 'nullable' => false,
00019 'commit' => SModel2::COMMIT_CHANGED,
00020 'type' => 'int',
00021 'foreign' => array('table' => 'SDRProject', 'on' => 'id', 'object' => 'project')
00022 ),
00023 'fieldId' => array(
00024 'nullable' => false,
00025 'commit' => SModel2::COMMIT_CHANGED,
00026 'type' => 'int',
00027 'foreign' => array('table' => 'SDRField', 'on' => 'id', 'object' => 'field')
00028 ),
00029 'indexOrder' => array(
00030 'nullable' => false,
00031 'type' => 'int'
00032 ),
00033 'required' => array(
00034 'type' => 'string'
00035 ),
00036 'project' => array(
00037 'commit' => SModel2::COMMIT_FOREIGN,
00038 'type' => 'SDRProject'
00039 ),
00040 'field' => array(
00041 'commit' => SModel2::COMMIT_FOREIGN,
00042 'type' => 'SDRField'
00043 )
00044 );
00045
00055 public function __construct($constraints = null, $multi = false) {
00056 parent::__construct();
00057
00058 $this->registerAttributes(self::$ATTRIBUTES);
00059 $this->registerTableName('SDRProjectField');
00060 $this->registerPrimaryKey(array('projectId', 'fieldId'), false);
00061
00062 if($constraints !== null)
00063 $this->populate($constraints, $multi);
00064 }
00065
00075 public static function getAttributesStatic() {
00076 return self::$ATTRIBUTES;
00077 }
00078
00084 public static function getTableNameStatic() {
00085 return 'SDRProjectField';
00086 }
00087
00099 public static function retrieve($constraints = array(), $checkOnly = false) {
00100 $list = self::getList($constraints, array(1), array(), $checkOnly);
00101 if(!$list || count($list) == 0)
00102 return $checkOnly ? false : null;
00103 else if($checkOnly)
00104 return $list > 0;
00105 else
00106 return $list[0];
00107 }
00108
00117 public static function exists($constraints = array()) {
00118 return self::retrieve($constraints, true);
00119 }
00120
00133 public static function retrieveMulti($constraints = array(), $checkOnly = false, $depth = 1) {
00134 $list = self::getListMulti($constraints, array(1), array(), $checkOnly, $depth);
00135 if(!$list || count($list) == 0)
00136 return $checkOnly ? false : null;
00137 else if($checkOnly)
00138 return $list > 0;
00139 else
00140 return $list[0];
00141 }
00142
00151 public static function existsMulti($constraints = array()) {
00152 return self::retrieveMulti($constraints, true);
00153 }
00154
00168 public static function getList($constraints = array(), $limit = array(), $order = array(), $count = false) {
00169 return parent::getListBase(self::getDBI(), $constraints, $limit, $order, $count, 'SDRProjectField', true);
00170 }
00171
00187 public static function getListMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00188 return parent::getListBaseMulti(self::getDBI(), $constraints, $limit, $order, $count,
00189 'SDRProjectField', true, $depth);
00190 }
00191
00192
00201 public function getParentProject() {
00202 return SDRProject::retrieve(array('id' => $this->projectId));
00203 }
00204
00214 public function getParentProjectMulti($depth = 1) {
00215 return SDRProject::retrieveMulti(array('Base.id' => $this->projectId), false, $depth);
00216 }
00217
00226 public function getParentField() {
00227 return SDRField::retrieve(array('id' => $this->fieldId));
00228 }
00229
00239 public function getParentFieldMulti($depth = 1) {
00240 return SDRField::retrieveMulti(array('Base.id' => $this->fieldId), false, $depth);
00241 }
00242
00243
00252 public static function findOrAddProjectField($fieldId, $projectId) {
00253 $DBI = self::getDBI();
00254 $results = self::getList(array('projectId' => $projectId, 'fieldId' => $fieldId));
00255 if ($results === false)
00256 return false;
00257
00258 if (count($results) > 0) {
00259 $projectField = $results[0];
00260
00261 } else {
00262
00263 $projectField = new SDRProjectField();
00264 $projectField->projectId = $projectId;
00265 $projectField->fieldId = $fieldId;
00266 $projectField->indexOrder = 0;
00267 $projectField->required = 0;
00268
00269 if (!$projectField->commit()) {
00270 self::setStaticError("Could not add field $name to $project");
00271 return false;
00272 }
00273 }
00274
00275 return $projectField;
00276 }
00277
00278 }
00279 ?>