/**
  * 
  */
 public function loadView()
 {
     parent::loadView();
     if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_GET) {
         $objects = null;
         if ($this->objectID()) {
             $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
             if ($object) {
                 $arr = A($object);
                 if ($this->shouldReturnObjects($arr)) {
                     $object->fireFault();
                     $objects = $arr;
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
             }
         } else {
             $request = new MFetchRequest($this->entity(), $this->context());
             if ($this->predicate()) {
                 $request->setPredicate($this->predicate());
             }
             $arr = $this->context()->executeFetchRequest($request);
             if ($arr->count() > 0) {
                 if ($this->shouldReturnObjects($arr)) {
                     $objects = $arr;
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
             }
         }
         if ($objects) {
             $xml = new MXMLDocumentView();
             $objectsXML = new MXMLEntityCollectionView($this->entity());
             foreach ($objects->toArray() as $object) {
                 $objectsXML->addManagedObject($object);
             }
             $xml->addSubview($objectsXML);
             $this->setView($xml);
         }
     } else {
         if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_PUT) {
             if ($this->objectID()) {
                 $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
                 if ($object) {
                     if ($this->shouldUpdateObject($object)) {
                         $this->_updateObjectWithInputData($object);
                         $this->context()->save();
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                     }
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
             }
         } else {
             if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_POST) {
                 if ($this->objectID()) {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 } else {
                     $object = $this->_createNewObjectWithInputData($this->entity());
                     if ($object) {
                         if ($this->shouldInsertObject($object)) {
                             $this->context()->save();
                             $this->setView(new MPlainTextView(Sf("%s/%s", $this->url(), N($object->objectID())->toString())));
                         } else {
                             $this->context()->deleteObject($object);
                             $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                         }
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_BAD_REQUEST);
                     }
                 }
             } else {
                 if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_DELETE) {
                     if ($this->objectID()) {
                         $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
                         if ($object) {
                             if ($this->shouldDeleteObject($object)) {
                                 $this->context()->deleteObject($object);
                                 $this->context()->save();
                             } else {
                                 $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                             }
                         } else {
                             $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
                         }
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                     }
                 }
             }
         }
     }
 }
 /**
  * Returns a Managed Object whose property matches the specified object value
  *
  * This method fetches an object from the Persistent Store that contains the
  * specified property and whose value for that property matches the value
  * specified in $value
  *
  * @param MEntityDescriptionProperty $property The property whose value you
  * wish to match against
  * @param MObject $value The value to be matched
  *
  * @return MManagedObject The fetched MManagedObject
  */
 public function objectWith(MEntityDescriptionProperty $property, MObject $value = null)
 {
     $request = new MFetchRequest($property->entity(), $this);
     if ($value) {
         $request->setPredicate(Sf("%s = '%s'", $property->name()->stringValue(), $value->toString()->stringValue()));
     } else {
         $request->setPredicate(Sf("%s = null", $property->name()->stringValue()));
     }
     $data = $this->executeFetchRequest($request);
     return $data->lastObject();
 }
 /**
  * @internal
  *
  * @return MArray
  */
 protected function executeFetchRequest(MFetchRequest $request)
 {
     $results = new MMutableArray();
     $query = new MMutableString();
     $query->appendString(Sf("SELECT `objectID` FROM `%s`", $request->entity()->plural()));
     if ($request->predicate() != null) {
         $query->appendString(Sf(" WHERE %s", $request->predicate()));
     }
     $query->appendString(S(";"));
     foreach ($this->connection()->query($query->stringValue()) as $row) {
         $object = $request->context()->newObjectForEntity($request->entity(), (int) $row['objectID']);
         $results->addObject($object);
     }
     return $results;
 }