get() public method

### Usage Get an article and some relationships: $article = $articles->get(1, ['contain' => ['Users', 'Comments']]);
public get ( $primaryKey, $options = [] )
 /**
  * testSendNewPasswordEmail
  *
  * @return void
  */
 public function testSendNewPasswordEmail()
 {
     $user = $this->Users->get(1);
     $this->Mailer->expects($this->once())->method('to')->with('*****@*****.**')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('subject')->with('Your new password')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('template')->with('Burzum/UserTools.Users/new_password')->will($this->returnSelf());
     $this->Mailer->expects($this->once())->method('set')->with('user', $user)->will($this->returnSelf());
     $this->Mailer->sendNewPasswordEmail($user);
 }
 /**
  * test WHERE conditions against unary expression.
  *
  * @return void
  */
 public function testUnaryExpression()
 {
     $this->table->addColumn('user-birth-date', ['type' => 'date'], false);
     $first = $this->table->get(1);
     $first->set('user-birth-date', time());
     $this->table->save($first);
     $second = $this->table->find('all', ['eav' => true])->where(['user-birth-date IS' => null])->order(['id' => 'ASC'])->first();
     $this->assertTrue(!empty($second) && $second->get('id') == 2);
 }
 /**
  * Move a node under the same parent node or under a new node.
  * New position of the node can be specified
  *
  * @param int $id ID of the node to move
  * @param int $parent_id ID of the (new) parent node
  * @param int $position New position of the node. Position is zero based.
  * @return boolean
  */
 public function moveNode($id, $parent_id, $position = null)
 {
     $primaryKey = $this->_table->schema()->primaryKey();
     $primaryKey = count($primaryKey) == 1 ? $primaryKey[0] : $primaryKey;
     $parent_id_fieldname = $this->config('model_parent_id_fieldname');
     $sort_fieldname = $this->config('model_sort_fieldname');
     $connection = $this->_table->connection();
     $connection->begin();
     $result = true;
     /*
      * Get moved node
      */
     $node = $this->_table->get($id);
     /*
      * Get current nodes positions of (new) siblings
      */
     $current_children = $this->_table->query()->where([$parent_id_fieldname => $parent_id])->order([$sort_fieldname => 'asc']);
     $new_sort_children = [];
     foreach ($current_children as $current_position => $current_child) {
         if ($current_child->{$primaryKey} != $id) {
             $new_sort_children[] = $current_child;
         }
     }
     /*
      * Default position is after all siblings
      */
     $position = isset($position) ? $position : $current_children->count();
     $position = $position >= 0 ? $position : 0;
     $position = $position <= count($new_sort_children) ? $position : count($new_sort_children);
     /*
      * Insert moved node at position
      */
     array_splice($new_sort_children, $position, 0, array($node));
     /*
      * If node has a new parent -> save it
      */
     if ($node->{$parent_id_fieldname} != $parent_id) {
         $query = $this->_table->query()->update()->set([$parent_id_fieldname => $parent_id])->where([$primaryKey => $id]);
         if (!$query->execute()) {
             $result = false;
         }
     }
     /*
      * Update positions
      */
     foreach ($new_sort_children as $index => $new_sort_child) {
         $query = $this->_table->query()->update()->set([$sort_fieldname => $index * 10])->where([$primaryKey => $new_sort_child->{$primaryKey}]);
         if (!$query->execute()) {
             $result = false;
         }
     }
     /***********/
     if ($result) {
         $connection->commit();
     } else {
         $connection->rollback();
     }
     return $result;
 }
Example #4
0
 public function testGET_view()
 {
     // 1. Obtain the relevant records and verify their referential integrity.
     $book_id = FixtureConstants::bookTypical;
     $distribution_id = FixtureConstants::distributionTypical;
     $distribution = $this->Distributions->get($distribution_id, ['contain' => ['Accounts.Categories', 'Currencies']]);
     $transaction_id = FixtureConstants::transactionTypical;
     $transaction = $this->Transactions->get($transaction_id);
     $this->assertEquals($distribution['transaction_id'], $transaction['id']);
     // 2. Submit request, examine response, observe no redirect, and parse the response.
     $this->get("/books/{$book_id}/transactions/{$transaction_id}/distributions/{$distribution_id}");
     $this->assertResponseCode(200);
     $this->assertNoRedirect();
     $dom = new \DomDocument();
     $dom->loadHTML($this->_response->body());
     $xpath = new \DomXPath($dom);
     // 3. Isolate the content produced by this controller method (excluding the layout.)
     $content_node = $this->getTheOnlyOne($xpath, "//div[@id='DistributionsView']");
     // 4. Count the A tags.
     $unknownATagCnt = $xpath->query(".//a", $content_node)->length;
     // 4.1 Look for the account distributions link
     //$this->getTheOnlyOne($xpath,"//a[@id='AccountDistributions']",$content_node);
     //$unknownATagCnt--;
     // 4.2 Ensure that all the <A> tags have been accounted for
     $this->assertEquals(0, $unknownATagCnt);
     // 5. Ensure that there is a suitably named table to display the results.
     $table_node = $this->getTheOnlyOne($xpath, "//table[@id='DistributionViewTable']", $content_node);
     // 5.1 Inspect the caption of the table.
     $this->assertContains("{$distribution_id}", $this->getTheOnlyOne($xpath, "caption", $table_node)->textContent);
     // 6. Now inspect the fields in the table.  We want to know that:
     // A. The correct fields are there and no other fields.
     // B. The fields have correct values.
     //
     // This is the count of the table rows that are presently unaccounted for.
     $unknownRowCnt = $xpath->query("//tr", $table_node)->length;
     // 6.1 drcr
     $expected = $distribution['drcr'] == 1 ? 'DR' : 'CR';
     $this->getTheOnlyOne($xpath, "//tr[1][@id='drcr']/td[text()='{$expected}']", $table_node);
     $unknownRowCnt--;
     // 6.2 category_title
     $expected = $distribution->account->catstring;
     $this->getTheOnlyOne($xpath, "//tr[2][@id='category_title']/td[text()='{$expected}']", $table_node);
     $unknownRowCnt--;
     // 6.3 account title
     $expected = $distribution->account->title;
     $this->getTheOnlyOne($xpath, "//tr[3][@id='account_title']/td[text()='{$expected}']", $table_node);
     $unknownRowCnt--;
     // 6.4 amount
     $this->getTheOnlyOne($xpath, "//tr[4][@id='amount']/td[text()='{$distribution->amount}']", $table_node);
     $unknownRowCnt--;
     // 6.5 currency symbol
     $expected = $distribution->currency->symbol;
     $this->getTheOnlyOne($xpath, "//tr[5][@id='currency_symbol']/td[text()='{$expected}']", $table_node);
     $unknownRowCnt--;
     // 6.9 Have all the rows been accounted for?  Are there any extras?
     $this->assertEquals(0, $unknownRowCnt);
 }
Example #5
0
 /**
  * Fetch a setting by keys
  * @param string $key
  * @param array $options
  * @return string
  */
 public function get($key, $options = [])
 {
     try {
         $setting = parent::get($key, $options);
         return (string) $setting;
     } catch (\Cake\Datasource\Exception\RecordNotFoundException $ex) {
         return '';
     }
 }
Example #6
0
 public function deleteCronAction()
 {
     $id = $_POST['id'] ?? null;
     if ($id) {
         try {
             $cron = $this->cronsTable->get($id);
             if (($_POST['deleteLog'] ?? false) == $id && $cron->output) {
                 $logFile = $this->site['logFolder'] . '/' . $cron->output;
                 if (!unlink($logFile)) {
                     $this->flasher->error("Could not delete log file: " . $logFile);
                 }
             }
             $this->cronsTable->delete($cron);
             $this->flasher->success('Successfully deleted cron!');
         } catch (\Exception $e) {
             $this->flasher->error("Could not delete cron: " . $e->getMessage());
         }
         $this->redirect('/admin/crons');
     }
 }
Example #7
0
 public function testGET_view()
 {
     // 1. Obtain the relevant records and verify their referential integrity.
     $account_id = FixtureConstants::accountTypical;
     $account = $this->Accounts->get($account_id);
     $book_id = FixtureConstants::bookTypical;
     $book = $this->Books->get($book_id);
     //$category_id=FixtureConstants::categoryTypical;
     //$category=$this->Categories->get($category_id);
     $this->assertEquals($account['book_id'], $book['id']);
     //$this->assertEquals($account['category_id'],$category['id']);
     // 2. Submit request, examine response, observe no redirect, and parse the response.
     $this->get('/books/' . $book_id . '/accounts/' . $account_id);
     $this->assertResponseCode(200);
     $this->assertNoRedirect();
     $dom = new \DomDocument();
     $dom->loadHTML($this->_response->body());
     $xpath = new \DomXPath($dom);
     // 3. Isolate the content produced by this controller method (excluding the layout.)
     $content_node = $this->getTheOnlyOne($xpath, "//div[@id='AccountsView']");
     // 4. Count the A tags.
     $unknownATagCnt = $xpath->query(".//a", $content_node)->length;
     // 4.1 Look for the account distributions link
     $this->getTheOnlyOne($xpath, "//a[@id='AccountDistributions']", $content_node);
     $unknownATagCnt--;
     // 4.2 Ensure that all the <A> tags have been accounted for
     $this->assertEquals(0, $unknownATagCnt);
     // 5. Ensure that there is a suitably named table to display the results.
     $table_node = $this->getTheOnlyOne($xpath, "//table[@id='AccountViewTable']", $content_node);
     // 5.1 Inspect the caption of the table.
     $this->assertContains("{$account_id}", $this->getTheOnlyOne($xpath, "caption", $table_node)->textContent);
     // 6. Now inspect the fields in the table.  We want to know that:
     // A. The correct fields are there and no other fields.
     // B. The fields have correct values.
     //
     // This is the count of the table rows that are presently unaccounted for.
     $unknownRowCnt = $xpath->query("//tr", $table_node)->length;
     // 6.1 book_title
     $this->getTheOnlyOne($xpath, "//tr[1][@id='book_title']/td[text()='{$book->title}']", $table_node);
     $unknownRowCnt--;
     // 6.2 category_title
     //$this->getTheOnlyOne($xpath,"//tr[2][@id='category_title']/td[text()='$category->title']",$table_node);
     $unknownRowCnt--;
     // 6.3 sort
     //$this->getTheOnlyOne($xpath,"//tr[3][@id='sort']/td[text()='$account->sort']",$table_node);
     //$unknownRowCnt--;
     // 6.4 title
     $this->getTheOnlyOne($xpath, "//tr[3][@id='title']/td[text()='{$account->title}']", $table_node);
     $unknownRowCnt--;
     // 6.9 Have all the rows been accounted for?  Are there any extras?
     $this->assertEquals(0, $unknownRowCnt);
 }
Example #8
0
 /**
  * Ensures that the provided entity contains non-empty values for the left and
  * right fields
  *
  * @param \Cake\ORM\Entity $entity The entity to ensure fields for
  * @return void
  */
 protected function _ensureFields($entity)
 {
     $config = $this->config();
     $fields = [$config['left'], $config['right']];
     $values = array_filter($entity->extract($fields));
     if (count($values) === count($fields)) {
         return;
     }
     $fresh = $this->_table->get($entity->get($this->_table->primaryKey()), $fields);
     $entity->set($fresh->extract($fields), ['guard' => false]);
     foreach ($fields as $field) {
         $entity->dirty($field, false);
     }
 }
Example #9
0
 /**
  * Toggle field value.
  *
  * @param Table|\Cake\ORM\Table $table
  * @param string|int $id
  * @param string|int $value
  * @param string $field
  * @throw BadRequestException
  * @throw RuntimeException
  */
 public function fieldToggle($table, $id, $value, $field = self::TOGGLE_DEFAULT_FIELD)
 {
     $this->_checkIsAjax();
     $this->_checkToggleData($id, $value);
     $this->_controller->viewBuilder()->layout('ajax')->templatePath('Common');
     $entity = $table->get($id);
     $entity->{$field} = !(int) $value;
     if ($result = $table->save($entity)) {
         $this->_controller->set('record', $result);
         $this->_controller->render('toggle');
     } else {
         throw new RuntimeException(__d('union', 'Failed toggling field {0} to {1}', $field, $entity->{$field}));
     }
 }
Example #10
0
 public function testGET_view()
 {
     // 1. Obtain the relevant records.
     $book_id = FixtureConstants::bookTypical;
     $book = $this->Books->get($book_id);
     // 2. Submit request, examine response, observe no redirect, and parse the response.
     $this->get("/books/{$book_id}");
     $this->assertResponseCode(200);
     $this->assertNoRedirect();
     $dom = new \DomDocument();
     $dom->loadHTML($this->_response->body());
     $xpath = new \DomXPath($dom);
     // 3. Isolate the content produced by this controller method (excluding the layout.)
     $content_node = $this->getTheOnlyOne($xpath, "//div[@id='BooksView']");
     // 4. Count the A tags.
     $unknownATagCnt = $xpath->query(".//a", $content_node)->length;
     // 4.1 Look for the accounts link
     $this->getTheOnlyOne($xpath, "//a[@id='BookAccounts']", $content_node);
     $unknownATagCnt--;
     // 4.2 Look for the transactions link
     $this->getTheOnlyOne($xpath, "//a[@id='BookTransactions']", $content_node);
     $unknownATagCnt--;
     // 4.3 Look for the balance sheet link
     $this->getTheOnlyOne($xpath, "//a[@id='BookBalanceSheet']", $content_node);
     $unknownATagCnt--;
     // 4.4 Look for the income statement link
     $this->getTheOnlyOne($xpath, "//a[@id='BookIncomeStatement']", $content_node);
     $unknownATagCnt--;
     // 4.5 Ensure that all the <A> tags have been accounted for
     $this->assertEquals(0, $unknownATagCnt);
     // 5. Ensure that there is a suitably named table to display the results.
     $table_node = $this->getTheOnlyOne($xpath, "//table[@id='BookViewTable']", $content_node);
     // 5.1 Inspect the caption of the table.
     $this->assertContains("{$book_id}", $this->getTheOnlyOne($xpath, "caption", $table_node)->textContent);
     // 6. Now inspect the fields in the table.  We want to know that:
     // A. The correct fields are there and no other fields.
     // B. The fields have correct values.
     //
     // This is the count of the table rows that are presently unaccounted for.
     $unknownRowCnt = $xpath->query("//tr", $table_node)->length;
     // 6.1 title
     $this->getTheOnlyOne($xpath, "//tr[1][@id='title']/td[text()='{$book->title}']", $table_node);
     $unknownRowCnt--;
     // 6.9 Have all the rows been accounted for?  Are there any extras?
     $this->assertEquals(0, $unknownRowCnt);
 }
Example #11
0
 public function testGET_view()
 {
     // 1. Obtain the relevant records.
     $category_id = FixtureConstants::categoryAsset;
     $category = $this->Categories->get($category_id);
     // 2. Submit request, examine response, observe no redirect, and parse the response.
     $this->get("/categories/{$category_id}");
     $this->assertResponseCode(200);
     $this->assertNoRedirect();
     $dom = new \DomDocument();
     $dom->loadHTML($this->_response->body());
     $xpath = new \DomXPath($dom);
     // 3. Isolate the content produced by this controller method (excluding the layout.)
     $content_node = $this->getTheOnlyOne($xpath, "//div[@id='CategoriesView']");
     // 4. Count the A tags.
     $unknownATagCnt = $xpath->query(".//a", $content_node)->length;
     // 4.9 Ensure that all the <A> tags have been accounted for
     $this->assertEquals(0, $unknownATagCnt);
     // 5. Ensure that there is a suitably named table to display the results.
     $table_node = $this->getTheOnlyOne($xpath, "//table[@id='CategoryViewTable']", $content_node);
     // 5.1 Inspect the caption of the table.
     $this->assertContains("{$category_id}", $this->getTheOnlyOne($xpath, "caption", $table_node)->textContent);
     // 6. Now inspect the fields in the table.  We want to know that:
     // A. The correct fields are there and no other fields.
     // B. The fields have correct values.
     //
     // This is the count of the table rows that are presently unaccounted for.
     $unknownRowCnt = $xpath->query("//tr", $table_node)->length;
     // 6.1 title
     $this->getTheOnlyOne($xpath, "//tr[1][@id='title']/td[text()='{$category->title}']", $table_node);
     $unknownRowCnt--;
     // 6.2 symbol
     $this->getTheOnlyOne($xpath, "//tr[2][@id='symbol']/td[text()='{$category->symbol}']", $table_node);
     $unknownRowCnt--;
     // 6.9 Have all the rows been accounted for?  Are there any extras?
     $this->assertEquals(0, $unknownRowCnt);
 }
Example #12
0
 /**
  * Test that an exception is raised when there are too many keys.
  *
  * @expectedException Cake\Datasource\Exception\InvalidPrimaryKeyException
  * @expectedExceptionMessage Record not found in table "articles" with primary key [1, 'two']
  * @return void
  */
 public function testGetExceptionOnTooMuchData()
 {
     $table = new Table(['name' => 'Articles', 'connection' => $this->connection, 'table' => 'articles']);
     $table->get([1, 'two']);
 }
Example #13
0
 /**
  * Get an article and some relationships:
  *
  * @param mixed $primaryKey
  * @param array $options
  * @return \Union\Core\ORM\Entity
  */
 public function get($primaryKey, $options = [])
 {
     return parent::get($primaryKey, $options);
 }
Example #14
0
 /**
  * Test that an exception is raised when there are not enough keys.
  *
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Incorrect number of primary key values. Expected 1 got 0.
  * @return void
  */
 public function testGetExceptionOnIncorrectData()
 {
     $table = new Table(['name' => 'Articles', 'connection' => $this->connection, 'table' => 'articles']);
     $table->get(null);
 }
Example #15
-1
 /**
  * Toggle ajax field.
  *
  * @param Table $table
  * @param $id
  * @param $value
  * @param string $field
  */
 public function fieldToggle(Table $table, $id, $value, $field = 'status')
 {
     $this->_controller->serializeAjax = false;
     if (empty($id) || $value === null) {
         throw new Exception(__d('union', 'Invalid content'));
     }
     $this->_controller->viewBuilder()->layout('ajax')->templatePath('Common');
     $record = $table->get($id);
     $record->{$field} = (int) (!$value);
     if ($entity = $table->save($record)) {
         $this->_controller->set('record', $entity);
         $this->_controller->render('Union/Core.toggle');
     } else {
         throw new Exception(__d('union', 'Failed toggling field {0} to {1}', $field, $record->{$field}));
     }
 }