ホームページ  >  記事  >  バックエンド開発  >  CakePHP の findById

CakePHP の findById

WBOY
WBOYオリジナル
2024-08-29 12:58:311045ブラウズ

CakePHP は、動的プログラミングの実装に使用されるオープンソース ツールです。開発者にさまざまなタイプのメソッドを提供します。 findbyid() は CakePHP メソッドの 1 つです。 findbyid() メソッドは、要件に従ってデータベースからデータを取得します。たとえば、場合によっては、特定のレコードを 1 秒以内に検索する必要があり、そのレコードの ID がわかっていれば、 findbyid() メソッドを使用して指定されたレコードを取得できます。つまり、複雑なコーディングを行わなくても、データベースから必要なレコードを簡単に取得できると言えます。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

CakePHP findById とは何ですか?

前に述べたように、モデル層の仕事の 1 つは、多数の機能から情報を取得することです。 CakePHP Model クラスには、この情報の検索、並べ替え、ページネーション、チャネリングを支援するいくつかの機能が付属しています。モデルで使用する最もよく知られた作品は、Model::find () です。

CakePHP の「検索」戦略により、データセットからの情報の回復が簡素化されます。 「検索」戦略を使用すると、多くのコードを作成することなく、非常に単純な問い合わせからより複雑な問い合わせまであらゆるものを開発できます。さらに、この戦略はほとんどの SQL タイプの要求に対処でき、より詳細な SQL の質問に拡張することができます。ここでは、「検索」戦略での作業の要点について、さまざまなモデルを見て回ります。

CakePHP findById 条件

次に、次のように findbyid のさまざまな条件を見てみましょう:

前に述べたように、モデル層の仕事の 1 つは、さまざまな能力から情報を取得することです。したがって、CakePHP Model クラスには、この情報の検索、並べ替え、ページネーション、チャネリングを支援するいくつかの機能が付属しています。

モデルで使用する最も広く認識されている作品は、Model: find() です。

構文:

find (string $specified type = 'specified type', array $params = array())

説明:

  • 上記の構文を使用すると、find ステートメントを実装できます。 Find は、すべてのモデルの情報回復能力を備えた多機能の主力製品です。 $type には、「all」、「first」、「count」、「list」、「neighbors」、または「strung」、または特徴付けできる任意のカスタム ロケーターを指定できます。
  • $type は大文字と小文字が区別されることに注意してください。大文字 (たとえば all) を使用すると、期待される結果が得られません。

$params は、さまざまな種類の find() にすべての境界を渡すために使用され、付随する考えられるキーを持ちます (もちろん、これらはすべて任意です)。

以下にさまざまな検索方法を示します:

1.検索(最初)

最初の結果を出力する必要がある場合、またはその時点でその 1 つの結果を言う必要がある場合は、このメソッドを使用できます。

構文:

find('first', $All parameter)

説明:

  • 上記の構文では、最初に find メソッドを使用します。また、すべてのパラメータを渡す必要があります。

例:

コード:

$result = $this -> Emp-> find('first');

説明:

  • 上記のステートメントを実行すると、Emp テーブルから 1 つのレコードが返されます。上記のステートメントの実装は、次のスクリーンショットを使用して説明されています。

出力:

CakePHP の findById

2.検索(カウント)

要件に従って整数値を返すために使用されます。

構文:

find('count', $All parameter)

説明:

  • 上記の構文では、find メソッドと count メソッドを使用しており、ここでもすべてのパラメーターを渡す必要があります。

例:

コード:

$result = $this -> Emp-> find('count');

説明:

  • 次のスクリーンショットを使用して、上記のステートメントの最終結果を示します。

出力:

CakePHP の findById

3. find('すべて')

これは、さまざまな可能な結果を​​返すために使用されます。実際、これはすべての find() バリエーションとページネーションで利用されるコンポーネントです。

構文:

find ('all', $All parameter)
find('list')

これは、順序付けされたクラスターを返すために使用され、入力選択ボックスに値を入力する場合など、概要が必要な場所に役立ちます。

構文:

find ('list', $All parameter)
find('threaded')

It is used to return a settled cluster and is fitting to utilize the parent_id field of your model information to fabricate settled outcomes.

Syntax:

find ('threaded', $All parameter)

So in this way, we can implement the find statement as per our requirement; we can also write the above statement inside the function, depending on the requirement.

How to use CakePHP findById?

Given below shows how we can use the findbyid method in CakePHP:

First, we need to create a table and insert records by using the following statement as follows:

Code:

CREATE TABLE IF NOT EXISTS `sampledemo` (
`id` char(30) NOT NULL,
`EmpName` varchar(250) DEFAULT NULL,
`EmpPass` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now insert records in the newly created table as follows.

Code:

INSERT INTO `sampledemo` (`id`, `EmpName`, `EmpPass`) VALUES
('3', 'Siya','$2y$10$HKLH3YiZE'),
('4', 'Rohan','$2y$10$bZcoCTW'),
('5', 'Tanya','$2y$10$SnGQV8O');

Explanation:

  • After executing the above query, we will get the following result, as shown in the screenshot.

Output:

CakePHP の findById

Code:

$results=$emp->find()
where(['id '=3])
toArray();

Explanation:

  • Using the above query, we will find all the records whose id equals 3.
  • We illustrate the final result of the above implementation using the following screenshot.

Output:

CakePHP の findById

Now let’s see another example for better understanding as follows:

Code:

$results=$emp->find()
where(['id '=4])
toArray();

Explanation:

  • Using the above query, we will find all the records whose id equals 4.
  • We illustrate the final result of the above implementation using the following screenshot.

Output:

CakePHP の findById

CakePHP findById Not Working

Given below shows what happens if the findbyid() method is not working:

  • The findbyid() method sometimes stops working because of the migration of CakePHP and the database.
  • In other words, we can say that A couple of CakePHP find() capacities quit working after a server movement from PHP 5.2 to 5.4 and MySQL 5.1 to 5.7.

Conclusion

From the above article, we have taken in the essential idea of the CakePHP findbyid, and we also see the representation and example of the CakePHP findById. This article showed us how and when to use the CakePHP findById.

以上がCakePHP の findByIdの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:CakePHP を含む次の記事:CakePHP を含む