All jobs

Move Yii2 Users From a Hardcoded Array to a Database Table

Yes. An agent turns the basic template's static User class into an ActiveRecord with a migration, seeds the two demo accounts, and keeps every login test passing.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

Every Yii2 basic app starts with the same placeholder, and the login page says so itself. In a repository laid out like yiisoft/yii2-app-basic, models/User.php extends BaseObject and keeps two accounts in a private static array: id 100, admin, and id 101, demo, each with a bcrypt passwordHash, an authKey and an accessToken. views/site/login.php tells visitors to log in with admin/admin or demo/demo and to edit the class to change that. The agent reads every caller before touching the class. models/LoginForm.php reads $user->passwordHash and calls User::findByUsername, which compares with strcasecmp, so usernames match without regard to case. config/web.php names the class as identityClass with enableAutoLogin on. The tests lean on the fixed data: tests/Unit/Models/UserTest.php looks up id 100, the token 100-token and the key test100key, and the functional and acceptance suites sign in as admin.

The rewrite keeps that contract and moves the storage. The agent creates a migration with php yii migrate/create create_user_table under a new migrations/ directory: id, a unique username, password_hash, auth_key, a unique access_token and two timestamps. A second migration inserts the two existing accounts with their current ids, hashes, keys and tokens copied from the array, so admin/admin and demo/demo still work after php yii migrate. models/User.php becomes a yii\db\ActiveRecord that still implements IdentityInterface. findIdentity and findIdentityByAccessToken become findOne calls, findByUsername compares lower-cased values so the case-insensitive match survives on any database, and passwordHash, authKey and accessToken stay readable as properties through getters, so LoginForm and its tests need no edit. New rows get an auth_key from Yii::$app->security->generateRandomString() in beforeSave, and a setPassword method hashes through the Security component that SiteController already receives by injection.

The harder part is the test setup, because until now nothing in this app opened a database. config/test_db.php points at a MySQL database named yii2basic_test, while .github/workflows/build.yml asks its shared workflow for the extensions dom, json, gd, imagick and docker-compose.yml defines a single php service, so neither CI path provides a MySQL server as written. The agent keeps config/db.php on MySQL and switches the test DSN to a SQLite file under runtime/, which requirements.php already lists as a supported driver, and states that choice in the pull request for the team to accept or reverse. It adds tests/Support/data/user.php with the two rows and a UserFixture class extending yii\test\ActiveFixture; the Unit suite's Yii2 module already enables the fixtures part in tests/Unit.suite.yml. The migrations run against the test database through tests/Support/bin/yii, wired into Codeception's bootstrap so composer tests stays one command. The hint text in the login view is updated to name the table. Then the agent runs composer tests, composer static and composer cs, and confirms the existing test files pass unmodified apart from loading the fixture.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Replace the hardcoded users in this Yii2 basic application with a database table. Read composer.json, models/User.php, models/LoginForm.php, controllers/SiteController.php, views/site/login.php, config/web.php, config/console.php, config/db.php, config/test.php, config/test_db.php, requirements.php, codeception.yml, the three suite files, every test that references User, tests/Support/bin/yii, docker-compose.yml and the workflows under .github first. Install PHP 8.2 or later with pdo_sqlite and pdo_mysql and Composer if the sandbox lacks them, and run composer install without committing the cookie validation key it writes into config/web.php. Add a migration creating a user table with id, unique username, password_hash, auth_key, unique access_token, created_at and updated_at, and a second migration inserting the two existing accounts with exactly the ids, hashes, auth keys and access tokens from the current array. Rewrite models/User.php as an ActiveRecord that implements IdentityInterface, keeps findByUsername case-insensitive, keeps passwordHash, authKey and accessToken readable under those names, generates an auth key on insert and has a setPassword method. Do not change LoginForm's public behaviour. Make the test suites run against a database that needs no server, load the two users through an ActiveFixture, and apply migrations to it as part of composer tests. Existing assertions must pass unchanged; add tests for a case-insensitive lookup, an unknown token and setPassword. Update the hint on the login page. Run composer tests, composer static and composer cs. Open a pull request that explains the test database choice, lists every file changed and quotes the three outputs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request adds two migrations, the fixture class and its data file, and rewrites models/User.php. It touches config/test_db.php, the Codeception bootstrap and one sentence in views/site/login.php. The description shows the table definition, states that the two seeded rows carry the same ids, hashes, keys and tokens as the array did, and quotes composer tests, composer static and composer cs. A separate section explains the SQLite test database and what it would take to test against MySQL in CI instead.

A reviewer starts with the seed migration, since it puts known credentials into a table. Those two accounts are the template's public demo logins, and the description says plainly that a real deployment should delete or rotate them; some teams will want the seed moved out of migrations and into the fixture alone. They check that UserTest, LoginFormTest, LoginFormCest and the acceptance LoginCest appear in the output with their assertions untouched in the diff, which is the evidence that the contract held. They also read findByUsername, because a case-sensitive lookup would pass every existing test and still change behaviour for anyone who types Admin.

What the pull request leaves out is listed too: no signup form, no password reset, no admin screen for users. Each builds on the table and is a separate request.

Run it on every app generated from the basic template

Agencies and teams that start many small Yii2 projects hit this step once per project. A script on your own server can send this body to POST /o/{org}/sessions for each new repository, with that repository in resources and a request_id that is a UUID v5 of the repository name, so running the script twice starts nothing new. Each run ends in a pull request on its own repository.

For a single long-lived app the event is the follow-up work. After this change merges, send the next request as a message to the same session, POST …/sessions/{id}/messages, while its sandbox is still parked: the database, the vendor directory and the agent's reading of the code are all still there for adding signup or password reset on top.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

This change is a contract-preserving rewrite, and the proof is a test suite that passed before and passes after against a real database. A Rush run has its own sandbox for that: the agent installs PHP, applies both migrations, loads the fixture and runs all three Codeception suites, with the PHP built-in server started by the php-builtin environment for the acceptance tests. Every command and its output is kept in the run's transcript, so the reviewer can see the suites pass on the old class first and on the new one after.

It is also work that goes better away from a developer's own environment. No local MySQL is created, Composer does not rewrite anyone's config/web.php, and the run can be handed a question and woken later with the answer, because the sandbox parks with its disk when a turn ends.

Frequently asked questions

Will the admin/admin and demo/demo logins still work?

Yes, after php yii migrate. The seed migration inserts both accounts with the same bcrypt hashes the array held, so the same passwords verify. The pull request flags that these are public demo credentials and that a deployed app should remove them.

Why do the property names stay camelCase when the columns are snake_case?

Because other code reads them. LoginForm::validatePassword uses $user->passwordHash, and the tests read username and call validateAuthKey. Getters on the ActiveRecord map the old names to the new columns, so the rewrite is confined to one class and the callers are untouched.

Why SQLite for tests if the app uses MySQL?

Because the repository's own CI does not start a MySQL server: the build workflow lists four PHP extensions and no database service, and the Compose file has one php container. A SQLite file keeps the suites runnable there. The agent names the trade-off, since a migration can behave differently on the two databases, and switching back is a one-line DSN change.

Does it add registration or password reset?

Not in this request. It adds setPassword and automatic auth keys so those features have what they need, and stops. Keeping the change to storage only is what lets the existing tests serve as proof that nothing else moved.

More Yii jobs

More jobs like this: Hand off a long task

Keep reading