Zend Framework (এখন Laminas) এ CRUD অপারেশনগুলি তৈরি (Create), পড়া (Read), আপডেট (Update) এবং মুছে ফেলা (Delete) মূলভাবে ডাটাবেস পরিচালনার সাথে সম্পর্কিত। CRUD অপারেশনগুলো একটি ওয়েব অ্যাপ্লিকেশনে ডাটার জীবনচক্র পরিচালনা করতে ব্যবহৃত হয়। এটি সাধারণত ডাটাবেসের সাথে যোগাযোগ করে, যেখানে ব্যবহারকারীর ইনপুট গ্রহণ এবং ডাটাবেসে সেই ডাটা ইনসার্ট, রিট্রিভ, আপডেট বা ডিলিট করা হয়।
এই গাইডে, Zend Framework এ CRUD অপারেশন কিভাবে বাস্তবায়ন করবেন তা বিস্তারিতভাবে দেখানো হবে।
১. Create (তৈরি করা)
Create অপারেশন ব্যবহার করে ডাটাবেসে নতুন রেকর্ড তৈরি করা হয়। সাধারণত এটি একটি ফর্ম থেকে ইনপুট নিয়ে ডাটাবেসে সেই তথ্য ইনসার্ট করে।
মডেল:
প্রথমে একটি Product মডেল তৈরি করা হবে যা ডাটাবেসের সাথে যোগাযোগ করবে।
namespace Product\Model;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
class ProductTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function addProduct($data)
{
$this->tableGateway->insert($data);
}
}
কন্ট্রোলার:
কন্ট্রোলারটি ফর্ম থেকে ইনপুট নেয় এবং ProductTable মডেলটি ব্যবহার করে ডাটাবেসে ডাটা ইনসার্ট করে।
namespace Product\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Product\Model\ProductTable;
class ProductController extends AbstractActionController
{
protected $productTable;
public function __construct(ProductTable $productTable)
{
$this->productTable = $productTable;
}
public function addAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$data = [
'name' => $request->getPost('name'),
'price' => $request->getPost('price'),
];
$this->productTable->addProduct($data);
return $this->redirect()->toRoute('product');
}
return new ViewModel();
}
}
ভিউ (Form):
এখন একটি ফর্ম তৈরি করতে হবে যেখানে ব্যবহারকারী পণ্য নাম এবং দাম প্রদান করবে।
<form method="post" action="/product/add">
<label for="name">Product Name</label>
<input type="text" name="name" required>
<label for="price">Price</label>
<input type="text" name="price" required>
<button type="submit">Add Product</button>
</form>
২. Read (পড়া)
Read অপারেশন ডাটাবেস থেকে ডাটা রিটার্ন করে এবং ভিউতে সেই ডাটা প্রদর্শন করে। সাধারণত এটি একটি রিড-অনলি অপারেশন যেখানে ইউজারের দেখার জন্য ডাটা ফেচ করা হয়।
মডেল:
namespace Product\Model;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
class ProductTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
}
কন্ট্রোলার:
namespace Product\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Product\Model\ProductTable;
class ProductController extends AbstractActionController
{
protected $productTable;
public function __construct(ProductTable $productTable)
{
$this->productTable = $productTable;
}
public function indexAction()
{
$products = $this->productTable->fetchAll();
return new ViewModel(['products' => $products]);
}
}
ভিউ (Display):
<h1>Product List</h1>
<ul>
<?php foreach ($this->products as $product): ?>
<li><?php echo $product->name . ' - $' . $product->price; ?></li>
<?php endforeach; ?>
</ul>
৩. Update (আপডেট করা)
Update অপারেশন ডাটাবেসে বিদ্যমান রেকর্ড আপডেট করতে ব্যবহৃত হয়। এটি সাধারণত ইউজার থেকে ইনপুট নিয়ে পুরোনো ডাটা পরিবর্তন করে।
মডেল:
namespace Product\Model;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
class ProductTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function updateProduct($id, $data)
{
$this->tableGateway->update($data, ['id' => $id]);
}
}
কন্ট্রোলার:
namespace Product\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Product\Model\ProductTable;
class ProductController extends AbstractActionController
{
protected $productTable;
public function __construct(ProductTable $productTable)
{
$this->productTable = $productTable;
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if ($id == 0) {
return $this->redirect()->toRoute('product');
}
$request = $this->getRequest();
if ($request->isPost()) {
$data = [
'name' => $request->getPost('name'),
'price' => $request->getPost('price'),
];
$this->productTable->updateProduct($id, $data);
return $this->redirect()->toRoute('product');
}
// এখানে ডাটাবেস থেকে পণ্যটির তথ্য এনে ফর্মে প্রি-পপুলেট করুন
return new ViewModel(['id' => $id]);
}
}
ভিউ (Edit Form):
<form method="post" action="/product/edit/<?php echo $id; ?>">
<label for="name">Product Name</label>
<input type="text" name="name" value="<?php echo $product->name; ?>" required>
<label for="price">Price</label>
<input type="text" name="price" value="<?php echo $product->price; ?>" required>
<button type="submit">Update Product</button>
</form>
৪. Delete (মুছে ফেলা)
Delete অপারেশন ব্যবহার করে ডাটাবেস থেকে রেকর্ড মুছে ফেলা হয়। সাধারণত এটি ইউজারের অনুরোধের ভিত্তিতে ডাটা রিমুভ করে।
মডেল:
namespace Product\Model;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
class ProductTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function deleteProduct($id)
{
$this->tableGateway->delete(['id' => $id]);
}
}
কন্ট্রোলার:
namespace Product\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Product\Model\ProductTable;
class ProductController extends AbstractActionController
{
protected $productTable;
public function __construct(ProductTable $productTable)
{
$this->productTable = $productTable;
}
public function deleteAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if ($id == 0) {
return $this->redirect()->toRoute('product');
}
$this->productTable->deleteProduct($id);
return $this->redirect()->toRoute('product');
}
}
সারাংশ
Zend Framework (Laminas) এ CRUD অপারেশনগুলি ডাটাবেসের সাথে কার্যকরভাবে কাজ করার জন্য ব্যবহৃত হয়। Create, Read, Update, এবং Delete অপারেশনগুলি অ্যাপ্লিকেশনটির প্রধান অংশ, যেখানে ইউজারের ইনপুটের ভিত্তিতে ডাটাবেসে পরিবর্তন করা হয়। এই অপারেশনগুলি যথাযথভাবে ইমপ্লিমেন্ট করার জন্য কন্ট্রোলার, মডেল এবং ভিউ ফাইলগুলির সমন্বয় প্রয়োজন।
Read more